时间:2021-07-01 10:21:17 帮助过:34人阅读
ClassTree.class.php
<?php /** * 无限分类树(支持子分类排序) * version:1.4 * author:Veris * website:www.mostclan.com */ class ClassTree { /** * 分类排序(降序) */ static public function sort($arr,$cols){ //子分类排序 foreach ($arr as $k => &$v) { if(!empty($v['sub'])){ $v['sub']=self::sort($v['sub'],$cols); } $sort[$k]=$v[$cols]; } if(isset($sort)) array_multisort($sort,SORT_DESC,$arr); return $arr; } /** * 横向分类树 */ static public function hTree($arr,$pid=0){ foreach($arr as $k => $v){ if($v['pid']==$pid){ $data[$v['id']]=$v; $data[$v['id']]['sub']=self::hTree($arr,$v['id']); } } return isset($data)?$data:array(); } /** * 纵向分类树 */ static public function vTree($arr,$pid=0){ foreach($arr as $k => $v){ if($v['pid']==$pid){ $data[$v['id']]=$v; $data+=self::vTree($arr,$v['id']); } } return isset($data)?$data:array(); } }
返回例子:
Array ( [4] => Array ( [id] => 4 [pid] => 0 [name] => 上海 [sort] => 2 ) [5] => Array ( [id] => 5 [pid] => 4 [name] => 闵行 [sort] => 0 ) [1] => Array ( [id] => 1 [pid] => 0 [name] => 浙江 [sort] => 0 ) [13] => Array ( [id] => 13 [pid] => 1 [name] => 金华 [sort] => 1 ) [10] => Array ( [id] => 10 [pid] => 1 [name] => 宁波 [sort] => 0 ) [6] => Array ( [id] => 6 [pid] => 10 [name] => 宁海 [sort] => 0 ) ) Array ( [4] => Array ( [id] => 4 [pid] => 0 [name] => 上海 [sort] => 2 [sub] => Array ( [5] => Array ( [id] => 5 [pid] => 4 [name] => 闵行 [sort] => 0 [sub] => Array ( ) ) ) ) [1] => Array ( [id] => 1 [pid] => 0 [name] => 浙江 [sort] => 0 [sub] => Array ( [13] => Array ( [id] => 13 [pid] => 1 [name] => 金华 [sort] => 1 [sub] => Array ( ) ) [10] => Array ( [id] => 10 [pid] => 1 [name] => 宁波 [sort] => 0 [sub] => Array ( [6] => Array ( [id] => 6 [pid] => 10 [name] => 宁海 [sort] => 0 [sub] => Array ( ) ) ) ) ) ) )
public function vTree($arr,$pid=0){ foreach($arr as $k => $v){ if($v['pid']==$pid){ $data[$v['id']]=$v; $data+=vTree($arr,$v['id']); } } return isset($data)?$data:array(); }
以上就是php无限分类树[支持子分类排序]的详细内容,更多请关注Gxl网其它相关文章!