菜单的无限极分类实现

2018-02-05 10:42:56来源:segmentfault作者:leo_qin人点击

分享
/**
* 递归
*/
public function getTree($data, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0)
{
$tree = array();
foreach($data as $k => $v) {
if($v[$pid] == $root) {
//父亲找到儿子
$v[$child] = $this->getTree($data, $pk, $pid, $child, $v[$pk]);
$tree[] = $v;
}
}
return $tree;
}
/**
* 迭代
*/
public function getTree($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;
}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台