<?php

$data = array (
  array (
    'id' => 4,
    'category_name' => '要闻',
    'pid' => 3,
    'created_at' => '2018-06-21 17:33:55',
    'updated_at' => '2018-06-21 17:33:55',
  ),
  array (
    'id' => 3,
    'category_name' => '新闻',
    'pid' => 0,
    'created_at' => '2018-06-21 17:33:45',
    'updated_at' => '2018-06-21 17:33:45',
  ),
  array (
    'id' => 5,
    'category_name' => '企业服务',
    'pid' => 3,
    'created_at' => '2018-06-21 17:34:12',
    'updated_at' => '2018-06-21 17:34:12',
  ),
  array (
    'id' => 6,
    'category_name' => '大新闻',
    'pid' => 0,
    'created_at' => '2018-06-26 14:41:28',
    'updated_at' => '2018-06-26 14:41:28',
  ),
  array (
    'id' => 7,
    'category_name' => '优惠',
    'pid' => 6,
    'created_at' => '2018-06-26 14:41:45',
    'updated_at' => '2018-06-26 14:41:45',
  ),
);
echo "<pre>";
//var_export($data);



function getTree($list,$pid=0,$level=0) {
  static $tree = array();
  foreach($list as $row) {
    if($row['parentID']==$pid) {
      $row['level'] = $level;
      $tree[] = $row;
      getTree($list, $row['id'], $level + 1);
    }
  }
  return $tree;
}

function makeTree($arr){
  $refer = array();
  $tree = array();
  foreach($arr as $k => $v){
    $refer[$v['id']] = & $arr[$k]; //创建主键的数组引用
  }
  foreach($arr as $k => $v){
    $pid = $v['pid'];  //获取当前分类的父级id
    if($pid == 0){
      $tree[] = & $arr[$k];  //顶级栏目
    }else{
      if(isset($refer[$pid])){
        $refer[$pid]['subcat'][] = & $arr[$k]; //如果存在父级栏目,则添加进父级栏目的子栏目数组中
      }
    }
  }
  return $tree;
}


$a = makeTree($data);
print_r($a);

$b = getTree($data);
print_r($b);

输出:

Array
(
    [0] => Array
        (
            [id] => 3
            [category_name] => 新闻
            [pid] => 0
            [created_at] => 2018-06-21 17:33:45
            [updated_at] => 2018-06-21 17:33:45
            [subcat] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [category_name] => 要闻
                            [pid] => 3
                            [created_at] => 2018-06-21 17:33:55
                            [updated_at] => 2018-06-21 17:33:55
                        )

                    [1] => Array
                        (
                            [id] => 5
                            [category_name] => 企业服务
                            [pid] => 3
                            [created_at] => 2018-06-21 17:34:12
                            [updated_at] => 2018-06-21 17:34:12
                        )

                )

        )

    [1] => Array
        (
            [id] => 6
            [category_name] => 大新闻
            [pid] => 0
            [created_at] => 2018-06-26 14:41:28
            [updated_at] => 2018-06-26 14:41:28
            [subcat] => Array
                (
                    [0] => Array
                        (
                            [id] => 7
                            [category_name] => 优惠
                            [pid] => 6
                            [created_at] => 2018-06-26 14:41:45
                            [updated_at] => 2018-06-26 14:41:45
                        )

                )

        )

)
Array
(
    [0] => Array
        (
            [id] => 4
            [category_name] => 要闻
            [pid] => 3
            [created_at] => 2018-06-21 17:33:55
            [updated_at] => 2018-06-21 17:33:55
            [level] => 0
        )

    [1] => Array
        (
            [id] => 3
            [category_name] => 新闻
            [pid] => 0
            [created_at] => 2018-06-21 17:33:45
            [updated_at] => 2018-06-21 17:33:45
            [level] => 0
        )

    [2] => Array
        (
            [id] => 5
            [category_name] => 企业服务
            [pid] => 3
            [created_at] => 2018-06-21 17:34:12
            [updated_at] => 2018-06-21 17:34:12
            [level] => 0
        )

    [3] => Array
        (
            [id] => 6
            [category_name] => 大新闻
            [pid] => 0
            [created_at] => 2018-06-26 14:41:28
            [updated_at] => 2018-06-26 14:41:28
            [level] => 0
        )

    [4] => Array
        (
            [id] => 7
            [category_name] => 优惠
            [pid] => 6
            [created_at] => 2018-06-26 14:41:45
            [updated_at] => 2018-06-26 14:41:45
            [level] => 0
        )

)

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注