PHP Infinite Pole classification

Source: Internet
Author: User
Infinite Pole classification: recursive and iterative implementations

1, find the sub-column of the designated column

2, find the descendants of the designated columns, both descendants of the tree

3, find the parent directory of the specified column, grandfather directory 、....,、 top-level directory

Raw data to be processed
$area =array (Array (' ID ' =>1, ' name ' = ' Shandong ', ' parent ' =>0), array (' ID ' =>2, ' name ' = ' Heze ', ' parent ' =>1), Array (' ID ' =>3, ' name ' = ' Dingtao ', ' parent ' =>2), array (' ID ' =>4, ' name ' = ' Qingdao ', ' parent ' =>1), array (' id ' = >5, ' name ' = ' Lacey ', ' parent ' =>4), array (' ID ' =>6, ' name ' = ' Yantai ', ' parent ' =>1), array (' ID ' =>7, ' name ' = ' Nanshan ', ' parent ' =>6), array (' ID ' =>8, ' name ' = ' Imitate mountain ', ' parent ' =>3), array (' ID ' =>9, ' name ' = ' Rizhao ', ' Parent ' =>1), array (' ID ' =>10, ' name ' = ' Jinan ', ' parent ' =>1));

Find sub-column function Findson ($arr, $id =0) {//Find the value of the parent of an element in $arr equals $id is its subdirectory $sforeach ($arr as $v) {if ($v [' Parent ']== $id) {$ Sons[]= $v; }} return $sons;}
Looking for a descendant tree
//takes advantage of  static variable  function Findtree ($arr, $id, $lev =1) {static $subs =array (); foreach ($arr as     $V) {if ($v [' Parent ']== $id) {$v [' Lev ']= $lev; $subs []= $v; Findtree ($arr, $v [' id '], $lev + 1); }} return $subs;} Method Two, function Findtree ($arr, $id, $lev =1) {static $subs =array (), foreach ($arr as $v) {if ($v [' Parent ']== $id) {$v [' Lev ']=$ Lev     $subs []= $v; $subs = $subs +findtree ($arr, $v [' id '], $lev + 1); }} return $subs;} Method Three, function Findtree ($arr, $id, $lev =1) {$subs =array (), foreach ($arr as $v) {if ($v [' Parent ']== $id) {$v [' Lev ']= $lev;     Subs[]= $v; $subs = Array_merge ($subs, Findtree ($arr, $v [' id '], $lev + 1)); }} return $subs;} 
Use iterative method to find descendant tree function subtree ($arr, $parent =0) {$task =array ($parent);//Task Table $tree=array ();//Region table while (!empty ($task)) {$ Flag=false;foreach ($arr as $k = + $v) {if ($v [' Parent ']== $parent) {$tree []= $v; Array_push ($task, $v [' id ']);// The latest area ID into the task stack $parent= $v [' id '];unset ($arr [$k]);//find Unit unset$flag=true;}} if ($flag ==false) {array_pop ($task); $parent =end ($task);}} return $tree;}
===//Infinite Pole Classification//Family tree//=== method One, function Familytree ($arr, $id, $lev =1) {//static $tree =array (); $tree =array (); foreach ($arr As $v) {if ($v [' id ']== $id) {$v [' Lev ']= $lev;  Determine if the subclass exists parent directory if ($v [' parent '] > 0) {$tree =array_merge ($tree, Familytree ($arr, $v [' Parent '], $lev + 1))//familytree ($arr, $v [' parent '];   }  $tree []= $v; }} return $tree;} Method two,//use iterative method to find the genealogy tree function tree ($arr, $id) {$tree =array (), while ($id!==0) {foreach ($arr as $v) {    if ($v [' id ']== $id) {        $tree []= $v;    $id = $v [' parent '];    Break;}}}        return $tree;}

The above describes the PHP infinite pole classification, including static variables of the content, I hope that the PHP tutorial interested in a friend helpful.

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