PHP生成无限级菜单数据
<?php
/**
* 生成无限级分类
* @param $list 数据
* @param string $pk 主键ID
* @param string $pid 子ID
* @param string $son 子级字段
* @param int $root
* @return array
*/
protected function tree($list, $pk = 'id', $pid = 'pid', $son = 'son', $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]][$son][] = &$packData[$key];
}
}
return $tree;
}
?>
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »