数组:
代码如下:
$items = array(
array('id' => 1,'pid' => 0,'name' => '一级11' ),
array('id' => 11,'name' => 'www.jb51.cc 一级12' ),
array('id' => 2,'pid' => 1,'name' => '二级21' ),
array('id' => 10,'pid' => 11,'name' => '二级22' ),
array('id' => 3,'name' => '二级23' ),
array('id' => 12,'name' => '二级24' ),
array('id' => 13,'pid' => 12,'name' => '三级31' ),
array('id' => 9,'name' => '二级25' ),
);
array('id' => 1,'pid' => 0,'name' => '一级11' ),
array('id' => 11,'name' => 'www.jb51.cc 一级12' ),
array('id' => 2,'pid' => 1,'name' => '二级21' ),
array('id' => 10,'pid' => 11,'name' => '二级22' ),
array('id' => 3,'name' => '二级23' ),
array('id' => 12,'name' => '二级24' ),
array('id' => 13,'pid' => 12,'name' => '三级31' ),
array('id' => 9,'name' => '二级25' ),
);
函数:
代码如下:
function formatTree($array,$pid = 0){
$arr = array();
$tem = array();
foreach ($array as $v) {
if ($v['pid'] == $pid) {
$tem = formatTree($array,$v['id']);
//判断是否存在子数组
$tem && $v['son'] = $tem;
$arr[] = $v;
}
}
return $arr;
}
原文链接:https://www.f2er.com/php/25039.html$arr = array();
$tem = array();
foreach ($array as $v) {
if ($v['pid'] == $pid) {
$tem = formatTree($array,$v['id']);
//判断是否存在子数组
$tem && $v['son'] = $tem;
$arr[] = $v;
}
}
return $arr;
}