无限极分类递归

function getTree($data, $pid=0, $level = 0)

{

$list = array();

foreach ($data as $k => $v) {

if ($v['fid'] == $pid) {

$v['level'] = $level;

$v['name'] = $v['name'] . '(' . ($level + 1) . ')'; // 这里可以加个层级次数

$v['children'] = getTree($data, $v['id'], $level + 1);

if ($v['children'] == null) {

unset($v['children']);

}

$list[] = $v;

}

}

return $list;

}

$array = getTree($arr);

 

posted @ 2020-07-07 15:29  Smile☆  阅读(81)  评论(0编辑  收藏  举报