Rumah >pembangunan bahagian belakang >tutorial php >PHP递归实现无限级分类
本篇文章的内容是PHP递归实现无限级分类,现在分享给大家,有需要的朋友也可以参考一下,大家一起过来看看吧
$datasection = array( array('id' => 1, 'name' => '安徽', 'pid' => 0), array('id' => 2, 'name' => '北京', 'pid' => 0), array('id' => 3, 'name' => '海淀', 'pid' => 2), array('id' => 4, 'name' => '中关村', 'pid' => 3), array('id' => 5, 'name' => '合肥', 'pid' => 1), array('id' => 6, 'name' => '上地', 'pid' => 3), array('id' => 7, 'name' => '河北', 'pid' => 0), array('id' => 8, 'name' => '石家庄', 'pid' => 7), ); function getTree($data, $pId) { $tree = ''; foreach($data as $k => $v) { if($v['pid'] == $pId) { $v['pid'] = getTree($data, $v['id']); $tree[] = $v; unset($data[$k]); } } return $tree; } $tree = getTree($datasection, 0); print_r($tree);
相关推荐:
Atas ialah kandungan terperinci PHP递归实现无限级分类. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!