ThinkPHP框架如何将递归获取的无限级分类子分类数据转化为多维数组?

thinkphp框架如何将递归获取的无限级分类子分类数据转化为多维数组?

thinkphp框架中,我们可以通过递归的方式读取无限级分类的子分类。使用getchildarea()函数时,返回的是一维数组,如果您需要多维数组,可以采用以下方式改进:

function getchildarea($id){
    if(!$id){
        return;
    }
    static $area;
    $area = $area ?? new ppcommonmodelrea;
    $result = collection($area->where(['pid' => $id])->order('id desc')->select())->toarray();
    static $res = [];
    if($result){
        foreach ($result as $key => $val) {
            $val['children'] = getchildarea($val['id']); 
            $res[] = $val;
        }
    }
    return $res;
}

配合deal_list_to_tree2()方法,可以将一维数组转化为多维数组:

<?php
/**
 * 方法 deal_list_to_tree2,一维数组根据$parent_id的值转为多维数组
 *
 * @param array $data 待处理的一维数组
 * @param string $pkname 用于转化为多维数组的主键字段
 * @param string $pidname 用于转化为多维数组的字段(根据该字段值转换)
 * @param string $childname 子级的字段名
 * @param bool $is_empty_childrens 是否返回空的子数组(childrens[])(true:是,false:否)
 * @param string $rootid 根节点$pkname值
 *
 * @return array $new_data 返回处理好的(多层级)多维数组
 *
 */
function deal_list_to_tree2($data, $pkname='id', $pidname='parent_id', $childname='children_list', $is_empty_childrens=false, $rootid=''){
    $new_data = [];
    if(!empty($data)){
        foreach($data as $sordata){
            if(array_key_exists($childname, $sordata) && !empty($sordata[$childname])){
                $res = deal_list_to_tree2($data, $pkname, $pidname, $childname, $is_empty_childrens, $sordata[$pkname]);
            }else{
                if($sordata[$pidname] == $rootid){
                    if($sordata[$pkname] != $rootid){
                        $res = deal_list_to_tree2($data, $pkname, $pidname, $childname, $is_empty_childrens, $sordata[$pkname]);
                    }
                    if(!empty($res) && !$is_empty_childrens){
                        if(array_key_exists($childname, $sordata)) {
                            if(array_key_exists($childname, $sordata)){
                                for($i=0; $i < count($res); $i++){
                                    $sordata[$childname][] = $res[$i];
                                }
                            }else{
                                $sordata[$childname][] = $res;
                            }
                        }else{
                            $sordata[$childname] = $res;
                        }
                    }
                    $new_data[] = $sordata;
                }
            }
        }
    }
    return $new_data;
}

这样,返回的多维数组格式为:

[
  {
    "id": 1,
    "area_name": "安徽省",
    "pid": 0,
    "level": 1,
    "children": [
      {
        "id": 4,
        "area_name": "合肥市",
        "pid": 1,
        "level": 2,
        "children": [
          {
            "id": 7,
            "area_name": "肥东县",
            "pid": 4,
            "level": 3,
            "children": [
              {
                "id": 8,
                "area_name": "桃园镇",
                "pid": 7,
                "level": 4,
                "children": [
                  {
                    "id": 9,
                    "area_name": "八斗乡",
                    "pid": 8,
                    "level": 5
                  }
                ]
              },
              {
                "id": 10,
                "area_name": "长丰县",
                "pid": 4,
                "level": 3
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "id": 2,
    "area_name": "江苏省",
    "pid": 0,
    "level": 1
  },
  {
    "id": 3,
    "area_name": "江西省",
    "pid": 0,
    "level": 1,
    "children": [
      {
        "id": 5,
        "area_name": "南昌市",
        "pid": 3,
        "level": 2
      },
      {
        "id": 6,
        "area_name": "九江市",
        "pid": 3,
        "level": 2
      }
    ]
  }
]

以上就是ThinkPHP框架如何将递归获取的无限级分类子分类数据转化为多维数组?的详细内容,更多请关注其它相关文章!