PHP生成树

如果数据比较规律,简单使用for循环就能实现

function array_tree($array) {
    $result = array();
    $tmp = array();
    foreach($array as $item) {
      if($item['pid'] == 0) {
        $i = count($result);
        $result[$i] = $item;
        $id = $item['cid'];
        $tmp[$id] = &$result[$i];
      }else {
        $id = $item['cid'];
        $parent_id = $item['pid'];
        if(isset($tmp[$parent_id])){
          $parent = $tmp[$parent_id];
        }else {
          $parent = array();
        }
        if(isset($parent['children'])){
          $i = count($parent['children']);
        }else{
          $i = 0;
        }
        $tmp[$parent_id]['children'][$i] = $item;
        $tmp[$id] = &$tmp[$parent_id]['children'][$i];
      }
    }
    return $result;
  }
  
  $arr=Array(
    Array('cid' => 2,'cname' => '新闻','pid' => 0),
    Array('cid' => 4,'cname' =>'体育','pid' => 0),
    Array('cid' => 5,'cname' => '娱乐','pid' => 0),
    Array('cid' => 7,'cname' => '热点新闻','pid' =>2),
    Array('cid' => 8,'cname' => '小众新闻','pid' => 2),
    Array('cid' => 9,'cname' => '民谣新闻','pid' => 8),
  );
  $tree2 = array_tree($arr);
  print_r( $tree2);
  
  
  Array
(
    [0] => Array
        (
            [cid] => 2
            [cname] => 新闻
            [pid] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [cid] => 7
                            [cname] => 热点新闻
                            [pid] => 2
                        )

                    [1] => Array
                        (
                            [cid] => 8
                            [cname] => 小众新闻
                            [pid] => 2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [cid] => 9
                                            [cname] => 民谣新闻
                                            [pid] => 8
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [cid] => 4
            [cname] => 体育
            [pid] => 0
        )

    [2] => Array
        (
            [cid] => 5
            [cname] => 娱乐
            [pid] => 0
        )

)

如果比较混乱,使用递归的方式

function formatTree($array, $pid = 0,$level = 1,$field = array('pid'=>'pid','id'=>'id','children'=>'children')){
    $arr = array();
    foreach ($array as &$v) {
      if ($v[$field['pid']] == $pid) {
        $v['level'] = $level;
        $tem = formatTree($array, $v[$field['id']],$v['level']+1,$field);
        //判断是否存在子数组
        $tem && $v[$field['children']] = $tem;
        $arr[] = $v;
      }
    }
    return $arr;
}
$tree = formatTree( $arr,0,1, $field = array('pid'=>'pid','id'=>'cid','children'=>'children') );
print_r( $tree);


Array
(
    [0] => Array
        (
            [cid] => 2
            [cname] => 新闻
            [pid] => 0
            [level] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [cid] => 7
                            [cname] => 热点新闻
                            [pid] => 2
                            [level] => 2
                        )

                    [1] => Array
                        (
                            [cid] => 8
                            [cname] => 小众新闻
                            [pid] => 2
                            [level] => 2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [cid] => 9
                                            [cname] => 民谣新闻
                                            [pid] => 8
                                            [level] => 3
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [cid] => 4
            [cname] => 体育
            [pid] => 0
            [level] => 1
        )

    [2] => Array
        (
            [cid] => 5
            [cname] => 娱乐
            [pid] => 0
            [level] => 1
        )

)