懂你所需,做你所想
当前位置:

function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0) {

// 创建Tree

$tree = array();

if(is_array($list)) {

// 创建基于主键的数组引用

$refer = array();

foreach ($list as $key => $data) {

$refer[$data[$pk]] =& $list[$key];

}

foreach ($list as $key => $data) {

// 判断是否存在parent

$parentId = $data[$pid];

if ($root == $parentId) {

$tree[] =& $list[$key];

}else{

if (isset($refer[$parentId])) {

$parent =& $refer[$parentId];

$parent[$child][] =& $list[$key];

}

}

}

}

return $tree;

}

public  function findChild($arr){

static $tree=array();

foreach ($arr as $key=>$val){

$tree[]=$val;

if (isset($val['_child'])){

$this->findChild($val['_child']);

}

}

return $tree;

}