首页 >后端开发 >php教程 >PHP不使用递归的无限级分类_PHP教程

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

WBOY
WBOY原创
2016-07-12 09:03:411091浏览

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