PHP does not use recursive unlimited classification _ PHP Tutorial

Source: Internet
Author: User
PHP does not use recursive infinity classification. PHP does not use Recursive Infinite-level classification and does not use recursion to implement infinite-level classification. The performance of a simple test is a little better than recursion, but it is too complicated to write, or recursion is simple and convenient. point generation PHP does not use recursive unlimited classification.

There is no need for recursion to implement unlimited classification. The performance of a simple test is a little better than that of recursion, but it is too complicated to write. it is easy to do recursion.

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, 'Deep' => 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) {// if (isset ($ manages [$ 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;}

Limit does not use recursion to implement infinite classification. The performance of a simple test is a little better than that of recursion, but it is too complicated to write, or recursion is simple and convenient...

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.