php递归实现树状无限极分类
先来个效果图
递归方法
/** * [递归无限极分类树] * @param [array] $arr[被分类数组] * @param [int] $PID [栏目父id,递归+1] * @param [int] $level [分类树层级,递归+1] * @param [string] $type [图片的类型] * @return [array] */
function getTree($arr, $PID = 0, $level = 0){ static $list = []; foreach ($arr as $k => $v) { if ($v['pid'] == $PID) { $v['level'] = $level; //str_repeat 把字符串重复指定的次数 $v["cate_name"] = str_repeat(' ', $level) . '|----' . $v["cate_name"]; $list[] = $v; //移除该项,减少后续递归消耗 unset($arr[$k]); getTree($arr, $v["id"], $level + 1); } } return $list; }
到底了
下一篇
0