php中递归无限分类之-通过子栏目查找父栏目
1,'province'=>'福建省','parent_id'=>0), array('cat_id'=>2,'province'=>'龙岩市','parent_id'=>1), array('cat_id'=>3,'province'=>'广东省','parent_id'=>0), array('cat_id'=>4,'province'=>'佛山市','parent_id'=>3), array('cat_id'=>5,'province'=>'江西省','parent_id'=>0), array('cat_id'=>6,'province'=>'南昌市','parent_id'=>5), array('cat_id'=>7,'province'=>'赣州市','parent_id'=>5), array('cat_id'=>8,'province'=>'宁都县','parent_id'=>7), ); function son($arr,$cat_id){ static $list; foreach($arr as $v){ if($v['cat_id'] == $cat_id){ print_r($v); $list[] =$v; son($arr,$v['parent_id']); } } return $list; } $list = son($area,8); foreach($list as $v){ echo '--->',$v['province']; } /**执行结果源代码 Array ( [cat_id] => 8 [province] => 宁都县 [parent_id] => 7 ) Array ( [cat_id] => 7 [province] => 赣州市 [parent_id] => 5 ) Array ( [cat_id] => 5 [province] => 江西省 [parent_id] => 0 ) --->宁都县--->赣州市--->江西省 **/ ?>
分享标题:php中递归无限分类之-通过子栏目查找父栏目
当前地址:http://scyanting.com/article/gecpss.html