Home >Backend Development >PHP Tutorial >PHP does not use recursion for infinite classification_PHP tutorial

PHP does not use recursion for infinite classification_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-12 09:03:411134browse

PHP does not use recursive infinite classification

No need to recurse to achieve infinite classification. After a simple test, the performance is slightly better than recursion, but the writing is too complicated. Recursion is simpler and more convenient
PHP does not use recursion for infinite classification_PHP tutorial

Code:

<code class="hljs xml"><!--?php
$list = array(
    array('id'=-->1, &#39;pid&#39;=>0, &#39;deep&#39;=>0, &#39;name&#39;=>&#39;test1&#39;),
    array(&#39;id&#39;=>2, &#39;pid&#39;=>1, &#39;deep&#39;=>1, &#39;name&#39;=>&#39;test2&#39;),
    array(&#39;id&#39;=>3, &#39;pid&#39;=>0, &#39;deep&#39;=>0, &#39;name&#39;=>&#39;test3&#39;),
    array(&#39;id&#39;=>4, &#39;pid&#39;=>2, &#39;deep&#39;=>2, &#39;name&#39;=>&#39;test4&#39;),
    array(&#39;id&#39;=>5, &#39;pid&#39;=>2, &#39;deep&#39;=>2, &#39;name&#39;=>&#39;test5&#39;),
    array(&#39;id&#39;=>6, &#39;pid&#39;=>0, &#39;deep&#39;=>0, &#39;name&#39;=>&#39;test6&#39;),
    array(&#39;id&#39;=>7, &#39;pid&#39;=>2, &#39;deep&#39;=>2, &#39;name&#39;=>&#39;test7&#39;),
    array(&#39;id&#39;=>8, &#39;pid&#39;=>5, &#39;deep&#39;=>3, &#39;name&#39;=>&#39;test8&#39;),
    array(&#39;id&#39;=>9, &#39;pid&#39;=>3, &#39;deep&#39;=>2, &#39;name&#39;=>&#39;test9&#39;),
);
function resolve($list) {
    $newList = $manages = $deeps = $inDeeps = array();
    foreach ($list as $row) {
        $newList[$row[&#39;id&#39;]] = $row;
    }
    $list = null;
    foreach ($newList as $row) {
        if (! isset($manages[$row[&#39;pid&#39;]]) || ! isset($manages[$row[&#39;pid&#39;]][&#39;children&#39;][$row[&#39;id&#39;]])) {
            if ($row[&#39;pid&#39;] > 0 && ! isset($manages[$row[&#39;pid&#39;]][&#39;children&#39;])) $manages[$row[&#39;pid&#39;]] = $newList[$row[&#39;pid&#39;]];
            $manages[$row[&#39;pid&#39;]][&#39;children&#39;][$row[&#39;id&#39;]] = $row;
        }
        if (! isset($inDeeps[$row[&#39;deep&#39;]]) || ! in_array($row[&#39;id&#39;], $inDeeps[$row[&#39;deep&#39;]])) {
            $inDeeps[$row[&#39;deep&#39;]][] = array($row[&#39;pid&#39;], $row[&#39;id&#39;]);
        }
    }
    krsort($inDeeps);
    array_shift($inDeeps);
    foreach ($inDeeps as $deep => $ids) {
        foreach ($ids as $m) {
            // 存在子栏目的进行转移
            if (isset($manages[$m[1]])) {
                $manages[$m[0]][&#39;children&#39;][$m[1]] = $manages[$m[1]];
                $manages[$m[1]] = null;
                unset($manages[$m[1]]);
            }
        }
    }
    return $manages[0][&#39;children&#39;];
}</code>

Recursive implementation

<code class="hljs php">function resolve2(& $list, $pid = 0) {
    $manages = array();
    foreach ($list as $row) {
        if ($row[&#39;pid&#39;] == $pid) {
            $manages[$row[&#39;id&#39;]] = $row;
            $children = resolve2($list, $row[&#39;id&#39;]);
            $children && $manages[$row[&#39;id&#39;]][&#39;children&#39;] = $children;
        }
    }
    return $manages;
}</code>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1078284.htmlTechArticlePHP does not use recursion for infinite classification without recursion. After a simple test, the performance is slightly better than recursion. A little bit, but it’s too complicated to write, so recursion is simple and convenient...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn