>백엔드 개발 >PHP 튜토리얼 >PHP不使用递归的无限级分类_PHP教程

PHP不使用递归的无限级分类_PHP教程

WBOY
WBOY원래의
2016-07-12 09:03:411074검색

PHP不使用递归的无限级分类

不用递归实现无限级分类,简单测试了下性能比递归稍好一点点点,但写得太复杂了,还是递归简单方便点
PHP不使用递归的无限级分类

代码:

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

递归实现

<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不使用递归的无限级分类 不用递归实现无限级分类,简单测试了下性能比递归稍好一点点点,但写得太复杂了,还是递归简单方便点 代...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.