If you use php to write tree structure?

php query to get an array, and then you need to use this array to write the json interface.
Interface format:

[
    {
      "id": 4,
      "name": "",
      "open": false,
      "children": [
        {
          "id": 5,
          "name": "",
          "open": false,
          "checked": false
        },
        {
          "id": 6,
          "name": "",
          "open": false,
          "checked": false
        },
        {
          "id": 333,
          "name": "233333",
          "open": false,
          "checked": false
        }
      ],
      "checked": false
    },
    {
      "id": 10,
      "name": "",
      "open": false,
      "children": [
        {
          "id": 8,
          "name": "",
          "open": false,
          "children": [
            {
              "id": 40,
              "name": "",
              "open": false,
              
              "url": null,
              "title": "40",
              "checked": false,
              "level": 2,
              "check_Child_State": 0,
              "check_Focus": false,
              "checkedOld": false,
              "dropInner": false,
              "drag": false,
              "parent": false
            },
            {
              "id": 41,
              "name": "",
              "open": false,
              "checked": false
            },
            {
              "id": 42,
              "name": "",
              "open": false,
              "checked": false
            }
          ],
          "checked": false
        },
        {
          "id": 11,
          "name": "",
          "open": false,
          "checked": false
        },
        {
          "id": 13,
          "name": "",
          "open": false,
          "children": [
            {
              "id": 34,
              "name": "",
              "open": false,
              "checked": false
            },
            {
              "id": 37,
              "name": "",
              "open": false,
              "checked": false
            },
            {
              "id": 38,
              "name": "",
              "open": false,
              "checked": false
            }
          ],
          "checked": false
        },
        {
          "id": 15,
          "name": "",
          "open": false,
          "checked": false
        }
      ],
      "checked": false
    }
  ]

Array contents:

array(14) {
  [0]=>
  array(10) {
    ["id"]=>
    string(2) "30"
    ["name"]=>
    string(6) ""
    ["pid"]=>
    string(1) "0"
    ["level"]=>
    string(1) "1"
    ["childid"]=>
    string(97) "a:7:{i:0;s:2:"31";i:1;s:2:"53";i:2;s:2:"54";i:3;s:2:"55";i:4;s:2:"56";i:5;s:2:"61";i:6;s:2:"74";}"
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [1]=>
  array(10) {
    ["id"]=>
    string(2) "31"
    ["name"]=>
    string(5) "T2"
    ["pid"]=>
    string(2) "30"
    ["level"]=>
    string(1) "2"
    ["childid"]=>
    string(32) "a:2:{i:0;s:2:"67";i:1;s:2:"68";}"
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [2]=>
  array(10) {
    ["id"]=>
    string(2) "32"
    ["name"]=>
    string(7) "2"
    ["pid"]=>
    string(1) "0"
    ["level"]=>
    string(1) "1"
    ["childid"]=>
    string(71) "a:5:{i:0;s:2:"34";i:1;s:2:"57";i:2;s:2:"58";i:3;s:2:"59";i:4;s:2:"60";}"
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [3]=>
  array(10) {
    ["id"]=>
    string(2) "35"
    ["name"]=>
    string(7) "2"
    ["pid"]=>
    string(1) "0"
    ["level"]=>
    string(1) "1"
    ["childid"]=>
    string(32) "a:2:{i:0;s:2:"46";i:1;s:2:"64";}"
    ["sort"]=>
    string(1) "2"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [4]=>
  array(10) {
    ["id"]=>
    string(2) "46"
    ["name"]=>
    string(9) ""
    ["pid"]=>
    string(2) "35"
    ["level"]=>
    string(1) "2"
    ["childid"]=>
    string(0) ""
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [5]=>
  array(10) {
    ["id"]=>
    string(2) "53"
    ["name"]=>
    string(7) "3"
    ["pid"]=>
    string(2) "30"
    ["level"]=>
    string(1) "2"
    ["childid"]=>
    string(0) ""
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [6]=>
  array(10) {
    ["id"]=>
    string(2) "58"
    ["name"]=>
    string(9) ""
    ["pid"]=>
    string(2) "32"
    ["level"]=>
    string(1) "2"
    ["childid"]=>
    string(0) ""
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [7]=>
  array(10) {
    ["id"]=>
    string(2) "59"
    ["name"]=>
    string(9) ""
    ["pid"]=>
    string(2) "32"
    ["level"]=>
    string(1) "2"
    ["childid"]=>
    string(0) ""
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [8]=>
  array(10) {
    ["id"]=>
    string(2) "64"
    ["name"]=>
    string(6) ""
    ["pid"]=>
    string(2) "35"
    ["level"]=>
    string(1) "2"
    ["childid"]=>
    string(0) ""
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [9]=>
  array(10) {
    ["id"]=>
    string(2) "68"
    ["name"]=>
    string(7) "T"
    ["pid"]=>
    string(2) "31"
    ["level"]=>
    string(1) "3"
    ["childid"]=>
    string(32) "a:2:{i:0;s:2:"69";i:1;s:2:"72";}"
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [10]=>
  array(10) {
    ["id"]=>
    string(2) "67"
    ["name"]=>
    string(7) "T"
    ["pid"]=>
    string(2) "31"
    ["level"]=>
    string(1) "3"
    ["childid"]=>
    string(0) ""
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [11]=>
  array(10) {
    ["id"]=>
    string(2) "69"
    ["name"]=>
    string(7) "T"
    ["pid"]=>
    string(2) "68"
    ["level"]=>
    string(1) "4"
    ["childid"]=>
    string(19) "a:1:{i:0;s:2:"73";}"
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [12]=>
  array(10) {
    ["id"]=>
    string(2) "72"
    ["name"]=>
    string(7) "T"
    ["pid"]=>
    string(2) "68"
    ["level"]=>
    string(1) "4"
    ["childid"]=>
    string(0) ""
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
  [13]=>
  array(10) {
    ["id"]=>
    string(2) "73"
    ["name"]=>
    string(7) "T"
    ["pid"]=>
    string(2) "69"
    ["level"]=>
    string(1) "5"
    ["childid"]=>
    string(0) ""
    ["sort"]=>
    string(1) "0"
    ["status"]=>
    string(1) "0"
    ["type"]=>
    string(0) ""
    ["remark"]=>
    string(0) ""
    ["delete"]=>
    string(1) "0"
  }
}

problem description

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Dec.22,2021

if (!function_exists('list_to_tree')) {
    /**
    * Tree
    * @param array $list 
    * @param string $pid parent
    * @param string $level level
    * @return array
    * @author  <zuojiazi@vip.qq.com>
    */
   function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) 
   {
       // Tree
       $tree = array();
       if(is_array($list)) {
           // 
           $refer = array();
           foreach ($list as $key => $data) {
               $refer[$data[$pk]] =& $list[$key];
           }

           foreach ($list as $key => $data) {
               // parent
               $parentId =  $data[$pid];
               if ($root == $parentId) {
                   $tree[] =& $list[$key];
               }else{
                   if (isset($refer[$parentId])) {
                       $parent =& $refer[$parentId];
                       $parent[$child][] =& $list[$key];
                   }
               }
           }
       }
       return $tree;
   }
}
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-1be0a4b-3150d.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-1be0a4b-3150d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?