The content of this article is PHP recursive implementation of infinite classification. Now I share it with everyone. Friends in need can also refer to it. Let’s come and take a look.
$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);
Related recommendations:
php Infinite Classification development process and example analysis
The above is the detailed content of PHP recursively implements infinite classification. For more information, please follow other related articles on the PHP Chinese website!