Php output three-stage linkage data json format at one time

has the following data table

then output json data like this at once:

$.rawCitiesData = [
  {
    "name":"",
    "code":"id",
    "sub": [
      {
        "name": "",
        "code": "id",
        "sub":[
            {
              "name":"",
              "code":"110101"
            },
            {
              "name":"",
              "code":"110102"
            },
            {
              "name":"",
              "code":"110105"
            },
            {
              "name":"",
              "code":"110106"
            },
            {
              "name":"",
              "code":"110107"
            },
            {
              "name":"",
              "code":"110108"
            },
            {
              "name":"",
              "code":"110109"
            },
            {
              "name":"",
              "code":"110111"
            },
            {
              "name":"",
              "code":"110112"
            },
            {
              "name":"",
              "code":"110113"
            },
            {
              "name":"",
              "code":"110114"
            },
            {
              "name":"",
              "code":"110115"
            },
            {
              "name":"",
              "code":"110116"
            },
            {
              "name":"",
              "code":"110117"
            },
            {
              "name":"",
              "code":"110228"
            },
            {
              "name":"",
              "code":"110229"
            }
        ]
      }
    ]
  },
< H2 > only the id and name fields in the output data table do not need < / H2 >

data table description
pid=0 means top-level classification
child if equal to 1 means there are subordinate categories

also ask God to teach me how to write this php


two scenarios, the first is to use recursion to traverse all the data.
the second is to cycle from level 1 to level 3, because your data seems to have only three levels.


<?php
//  pid 
// 
$items = [
    [
        'name' => '',
        'code' => '1',
        'pid' => '0'
    ],
    [
        'name' => '',
        'code' => '2',
        'pid' => '1'
    ],
    [
        'name' => '',
        'code' => '110101',
        'pid' => '2'
    ],
    [
        'name' => '',
        'code' => '110102',
        'pid' => '2'
    ]
];
$generateTree = function ($items, $parentKey = 'pid') {
    $tree = $temp = [];
    foreach ($items as $item) {
        $temp[$item['code']] = $item;
    }
    foreach ($items as $item) {
        if (isset($temp[$item[$parentKey]])) {
            $temp[$item[$parentKey]]['sub'][] = &$temp[$item['code']];
        } else {
            $tree[] = &$temp[$item['code']];
        }
        //  pid 
        unset($temp[$item['code']][$parentKey]);
    }
    return $tree;
};

print_r($generateTree($items));
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b360b2-2bfd6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b360b2-2bfd6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?