//事例数组
public function index(){
$data =array(
array(‘id‘ =>‘1‘,‘name‘ =>‘衣服‘, ‘pid‘=>0),
array(‘id‘=>‘2‘, ‘name‘ =>‘上衣‘, ‘pid‘=>1),
array(‘id‘=>‘3‘,‘name‘ =>‘裤子‘, ‘pid‘=>1),
array(‘id‘=>‘4‘, ‘name‘ =>‘鞋子‘, ‘pid‘=>0),
array(‘id‘ =>‘5‘,‘name‘ =>‘男鞋‘, ‘pid‘=>4),
array(‘id‘=>‘6‘, ‘name‘ =>‘女鞋‘, ‘pid‘=>4),
array(‘id‘ =>‘7‘,‘name‘ =>‘皮鞋‘, ‘pid‘=>5),
array(‘id‘=>‘8‘, ‘name‘ =>‘高跟鞋‘, ‘pid‘=>6),
);
$res = $this->treetest($data, 0);
dump($res);
}
//递归函数
public function treetest($data,$id=0){
$newarray =array();
$array =array();
if(is_array($data)){
foreach ($data as $val){
if($val[‘pid‘] == $id){
$array =$this->treetest($data, $val[‘id‘]);
$array && $val[‘son‘] =$array;
$newarray[]=$val;
}
}
return $newarray;
}