PHP does not use recursive infinite class classification _php tutorial

Source: Internet
Author: User

PHP does not use recursive infinite class classification


Without recursive implementation of infinite class classification, a simple test of performance than recursion slightly better, but the writing is too complex, or recursive simple convenient point

Code:


  1, ' pid ' =>0, ' deep ' =>0, ' name ' = ' test1 '), array (' ID ' =>2, ' pid ' =>1, ' deep ' =>1, ' name ' = ' test2 '), Array (' ID ' =>3, ' pid ' =>0, ' deep ' =>0, ' name ' = ' test3 '), array (' ID ' =>4, ' pid ' =>2, ' deep ' =>2, ' name ' = ' test4 '), array (' ID ' =>5, ' pid ' =>2, ' deep ' =>2, ' name ' = ' test5 '), array (' ID ' =>6, ' pid ' =>0, ' Dee P ' =>0, ' name ' = ' Test6 '), array (' ID ' =>7, ' pid ' =>2, ' deep ' =>2, ' name ' = ' test7 '), array (' ID ' =>8, ' PID ' =>5, ' deep ' =>3, ' name ' = ' test8 '), array (' ID ' =>9, ' pid ' =>3, ' deep ' =>2, ' name ' = ' Test9 '),);    Function Resolve ($list) {$newList = $manages = $deeps = $inDeeps = Array ();    foreach ($list as $row) {$newList [$row [' id ']] = $row;    } $list = null; foreach ($newList as $row) {if (! isset ($manages [$row [' pid]]) | |! isset ($manages [$row] [' pid ']][' Children '] [$row [' ID '])) {if ($row [' pid '] > 0 && isset ($manages [$row [' pid ']][' Children ')) $manages[$row [' pid '] = $newList [$row [' PID '];        $manages [$row [' pid ']][' Children '] [$row [' id ']] = $row; } if (! isset ($inDeeps [$row [' deep ']]) | |! In_array ($row [' id '], $inDeeps [$row [' deep ']]) {$inDeeps [$row [' Deep ']]        [] = Array ($row [' pid '], $row [' id ']);    }} krsort ($inDeeps);    Array_shift ($inDeeps); foreach ($inDeeps as $deep + $ids) {foreach ($ids as $m) {//Isset ($mana) for sub-columns                ges[$m [1]]) {$manages [$m [0]][' Children '] [$m [1]] = $manages [$m [1]];                $manages [$m [1]] = NULL;            Unset ($manages [$m [1]]); }}} return $manages [0][' Children '];}

Recursive implementation

function resolve2(& $list, $pid = 0) {    $manages = array();    foreach ($list as $row) {        if ($row['pid'] == $pid) {            $manages[$row['id']] = $row;            $children = resolve2($list, $row['id']);            $children && $manages[$row['id']]['children'] = $children;        }    }    return $manages;}

http://www.bkjia.com/PHPjc/1078284.html www.bkjia.com true http://www.bkjia.com/PHPjc/1078284.html techarticle PHP does not use recursive infinite class classification without recursive implementation of infinite class, a simple test of performance than recursion slightly better a little bit, but the writing is too complex, or recursive simple convenient point generation ...

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