首页 >后端开发 >php教程 > php多级归类查询

php多级归类查询

WBOY
WBOY原创
2016-06-13 13:16:061320浏览

php多级分类查询
http://www.oschina.net/question/167679_56466 类似这种效果,要怎么实现,还是有更好的方法

------解决方案--------------------

PHP code
$a = Array(
  '0' => Array('id' => 3,'sid' => 0),
  '1' => Array('id' => 4,'sid' => 0),
  '2' => Array('id' => 5,'sid' => 3),
  '3' => Array('id' => 6,'sid' => 0),
  '4' => Array('id' => 7,'sid' => 4),
  '5' => Array('id' => 8,'sid' => 0),
);

function cmsort($ar, $sid=0, &$res=array()) {
  foreach($ar as $r) {
    if($r['sid'] == $sid) {
      $res[] = $r;
      cmsort($ar, $r['id'], $res);
    }
  }
  return $res;
}

var_export(cmsort($a)); <div class="clear">
                 
              
              
        
            </div>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn