Turn list with parent ID into tree, id list Tree _php tutorial

Source: Internet
Author: User

Turn list with parent ID to tree, ID list to tree


We know that databases generally store trees in a list (id,pid). How to extract the tree? The simplest method is to check the table according to the PID loop. But there is no doubt that this creates a huge database query overhead.

Then the general recommended method is to isolate all relevant data at once, but this involves a problem, how to build a tree quickly.

I used to think that this is a complex operation that requires at least one recursion, and the time complexity will not be O (n).

Some time ago, a job needs to solve this problem. I thought about it and found that it was possible to solve this problem through a single-layer loop, which was achieved as follows:

1 functionList2tree ($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);Ten     }        One              A     foreach($map  as $id= &$item){ -         $pid=$item[$PIDX]; -         $item[$childKey] =Array(); the  -         if(!isset($map[$pid])){ -             $pMap[$id] = &$item;  -         }        +         Else{    -             $pItem= &$map[$pid]; +             $pItem[$childKey[] = &$item; A         }    at  -         unset($item,$pItem); -     } -     -     return Array_shift($pMap); -}

Test it:

1 //path for easy identification of parent-child relationships2 $json= <<<JSON3 [4     {5"id": 2,6"pid": 1,7"Path": "/se"8},9     {Ten"id": 3, One"pid": 2, A"Path": "/se/4901" -}, -     { the"id": 4, -"pid": 5, -"Path": "/se/4901/mask/query" -}, +         { -"id": 5, +"pid": 3, A"Path": "/se/4901/mask" at}, -     { -"id": 6, -"pid": 2, -"Path": "/se/4902" -}, in     { -"id": 7, to"pid": 6, +"Path": "/se/4902/mask" -     } the ] * JSON; $ Panax Notoginseng $list= Json_decode ($json,true); -  the Var_dump(List2tree ($list));

Results:

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(+) "/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(+) "/se/4902/mask"          ["List"]=>Array(0) {          }        }      }    }  }}

Succeeded in turning the list into a tree

http://www.bkjia.com/PHPjc/1102845.html www.bkjia.com true http://www.bkjia.com/PHPjc/1102845.html techarticle Convert list with parent ID to tree, ID list to tree we know that a database is generally saved as a list (id,pid). How to extract the tree? The simplest way to do this is by ...

  • 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.