Convert the list containing the parent ID to a tree, and the id list to a tree _ PHP Tutorial

Source: Internet
Author: User
Convert the list containing the parent ID into a tree, and the id list into a tree. Convert the list containing the parent ID into a tree, and the id list into a tree. we know that the database stores the tree in the form of a list (id, pid. How can we extract this tree? The simplest way is to convert the list containing the parent ID into a tree, and the id list into a tree.

We know that databases generally store trees in the form of a list (id, pid. How can we extract this tree? The simplest way is to look up the table based on the pid loop. But there is no doubt that this will produce a huge database query overhead.

Generally, the recommended method is to retrieve all the relevant data at a time, but this involves a problem: how to quickly build a tree.

I have always thought that this is a complicated operation and requires at least one Recursion. The time complexity is not O (n ).

Some time ago, a work requirement needs to be solved. I thought about it and found that this problem can be solved through a single-layer loop. the implementation is as follows:

 1 function list2Tree($listItem, $idx = 'id', $pIdx = 'pid', $childKey= 'list'){ 2     $map = array(); 3     $pMap = array(); 4  5     foreach($listItem as $item){ 6         $id = $item[$idx]; 7         $pid = $item[$pIdx]; 8         $map[$id] = &$item; 9         unset($item);10     }       11             12     foreach($map as $id => &$item){13         $pid = $item[$pIdx];14         $item[$childKey] = array();15 16         if(! isset($map[$pid])){17             $pMap[$id] = &$item; 18         }       19         else{   20             $pItem= &$map[$pid];21             $pItem[$childKey][] = &$item;22         }   23 24         unset($item, $pItem);25     }26    27     return array_shift($pMap);28 }

Test:

1 // The PATH helps identify parent-child relationships. 2 $ json = <
 
  

Result:

array(4) {  ["id"]=>  int(2)  ["pid"]=>  int(1)  ["path"]=>  string(3) "/se"  ["list"]=>  array(2) {    [0]=>    array(4) {      ["id"]=>      int(3)      ["pid"]=>      int(2)      ["path"]=>      string(8) "/se/4901"      ["list"]=>      array(1) {        [0]=>        array(4) {          ["id"]=>          int(5)          ["pid"]=>          int(3)          ["path"]=>          string(13) "/se/4901/mask"          ["list"]=>          array(0) {          }        }      }    }    [1]=>    array(4) {      ["id"]=>      int(6)      ["pid"]=>      int(2)      ["path"]=>      string(8) "/se/4902"      ["list"]=>      array(1) {        [0]=>        array(4) {          ["id"]=>          int(7)          ["pid"]=>          int(6)          ["path"]=>          string(13) "/se/4902/mask"          ["list"]=>          array(0) {          }        }      }    }  }}

The list is successfully converted into a tree.

Databases we know that databases generally store trees in the form of a list (id, pid. How can we extract this tree? The simplest method is according...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.