PHP生成无限级菜单数据

2018-02-07T22:21:00
<?php
   /**
     * 生成无限级分类
     * @param $list 数据
     * @param string $pk 主键ID
     * @param string $pid 子ID
     * @param string $son 子级字段
     * @param int $root
     * @return array
     */
    protected function tree($list,&nbsp;$pk = 'id', $pid&nbsp;=&nbsp;&#39;pid&#39;,&nbsp;$son = 'son', $root = 0)
    {
        $tree     = array();
        $packData = array();
        foreach ($list&nbsp;as&nbsp;$data) {
            $packData[$data[$pk]]&nbsp;=&nbsp;$data;
        }
        foreach ($packData&nbsp;as&nbsp;$key => $val) {
            if ($val[$pid] == $root) {
                $tree[]&nbsp;=&nbsp;&amp;$packData[$key];
            } else {
                $packData[$val[$pid]][$son][] = &$packData[$key];
            }
        }
        return $tree;
    }
?>
当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »