创建一个菜单无限极分类方法
function generateTree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) { $tree = array(); $packData = array(); foreach ($list as $data) { $packData[$data[$pk]] = $data; } foreach ($packData as $key => $val) { if ($val[$pid] == $root) { //代表跟节点, 重点一 $tree[] = &$packData[$key]; } else { //找到其父类,重点二 $packData[$val[$pid]][$child][] = &$packData[$key]; } } return $tree; }
$items = array( 1 => array('id' => 1, 'pid' => 0, 'name' => '安徽省'), 2 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'), 3 => array('id' => 3, 'pid' => 1, 'name' => '合肥市'), 4 => array('id' => 4, 'pid' => 3, 'name' => '长丰县'), 6 => array('id' => 6, 'pid' => 4, 'name' => '小溪村'), 5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'), );