php数组递归方法
有如下php数组:
-
-
function genTree5($items) {
- foreach ($items as $item)
- $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
- return isset($items[0]['son']) ? $items[0]['son'] : array();
- }
复制代码
方法二:
-
-
function findChild($arr,$id){
- $childs=array();
- foreach ($arr as $k => $v){
- if($v['pid']== $id){
- $childs[]=$v;
- }
- }
- // echo "
";print_r($childs);die();
- return $childs;
- }
function build_tree($root_id){
- global $items;
- $childs =array();
- $childs=findChild($items,$root_id);
- // print_r($childs);
- // die();
- if(empty($childs)){
- return null;
- }
- foreach ($childs as $k => $v){
- $rescurTree=build_tree($v['id']);
- if( null != $rescurTree){
- $childs[$k]['son']=$rescurTree;
- }
- }
- return $childs;
- }
-
复制代码
|