首页 > 后端开发 > php教程 > PHP树-不需要递归,php树递归_PHP教程

PHP树-不需要递归,php树递归_PHP教程

WBOY
发布: 2016-07-12 08:50:03
原创
872 人浏览过

PHP树-不需要递归,php树递归

<span>/*</span><span>*
 * 创建父节点树形数组
 * 参数
 * $ar 数组,邻接列表方式组织的数据
 * $id 数组中作为主键的下标或关联键名
 * $pid 数组中作为父键的下标或关联键名
 * 返回 多维数组
 *</span><span>*/</span>
<span>function</span> find_parent(<span>$ar</span>, <span>$id</span>='id', <span>$pid</span>='pid'<span>) {
  </span><span>foreach</span>(<span>$ar</span> <span>as</span> <span>$v</span>) <span>$t</span>[<span>$v</span>[<span>$id</span>]] = <span>$v</span><span>;
  </span><span>foreach</span> (<span>$t</span> <span>as</span> <span>$k</span> => <span>$item</span><span>){
    </span><span>if</span>( <span>$item</span>[<span>$pid</span><span>] ){
      </span><span>if</span>( ! <span>isset</span>(<span>$t</span>[<span>$item</span>[<span>$pid</span>]]['parent'][<span>$item</span>[<span>$pid</span><span>]]) )
         </span><span>$t</span>[<span>$item</span>[<span>$id</span>]]['parent'][<span>$item</span>[<span>$pid</span>]] =& <span>$t</span>[<span>$item</span>[<span>$pid</span><span>]];
    }
  }
  </span><span>return</span> <span>$t</span><span>;
}


</span><span>/*</span><span>*
 * 创建子节点树形数组
 * 参数
 * $ar 数组,邻接列表方式组织的数据
 * $id 数组中作为主键的下标或关联键名
 * $pid 数组中作为父键的下标或关联键名
 * 返回 多维数组
 *</span><span>*/</span>
<span>function</span> find_child(<span>$ar</span>, <span>$id</span>='id', <span>$pid</span>='pid'<span>) {
  </span><span>foreach</span>(<span>$ar</span> <span>as</span> <span>$v</span>) <span>$t</span>[<span>$v</span>[<span>$id</span>]] = <span>$v</span><span>;
  </span><span>foreach</span> (<span>$t</span> <span>as</span> <span>$k</span> => <span>$item</span><span>){
    </span><span>if</span>( <span>$item</span>[<span>$pid</span><span>] ) {
      </span><span>$t</span>[<span>$item</span>[<span>$pid</span>]]['child'][<span>$item</span>[<span>$id</span>]] =& <span>$t</span>[<span>$k</span><span>];
    }
  }
  </span><span>return</span> <span>$t</span><span>;
}

    </span><span>$data</span> = <span>array</span><span>(
      </span><span>array</span>('ID'=>1, 'PARENT'=>0, 'NAME'=>'祖父'),
      <span>array</span>('ID'=>2, 'PARENT'=>1, 'NAME'=>'父亲'),
      <span>array</span>('ID'=>3, 'PARENT'=>1, 'NAME'=>'叔伯'),
      <span>array</span>('ID'=>4, 'PARENT'=>2, 'NAME'=>'自己'),
      <span>array</span>('ID'=>5, 'PARENT'=>4, 'NAME'=>'儿子'),<span>
    );

    </span><span>$p</span> = find_parent(<span>$data</span>, 'ID', 'PARENT'<span>);
    </span><span>$c</span> = find_child(<span>$data</span>, 'ID', 'PARENT'<span>);
    </span><span>Print_r</span> (<span>$c</span>);
登录后复制
登录后复制
登录后复制

执行效果:

登录后复制
登录后复制
<span>Array</span><span>
(
    [</span>1] => <span>Array</span><span>
        (
            [ID] </span>=> 1<span>
            [PARENT] </span>=> 0<span>
            [NAME] </span>=><span> 祖父
            [child] </span>=> <span>Array</span><span>
                (
                    [</span>2] => <span>Array</span><span>
                        (
                            [ID] </span>=> 2<span>
                            [PARENT] </span>=> 1<span>
                            [NAME] </span>=><span> 父亲
                            [child] </span>=> <span>Array</span><span>
                                (
                                    [</span>4] => <span>Array</span><span>
                                        (
                                            [ID] </span>=> 4<span>
                                            [PARENT] </span>=> 2<span>
                                            [NAME] </span>=><span> 自己
                                            [child] </span>=> <span>Array</span><span>
                                                (
                                                    [</span>5] => <span>Array</span><span>
                                                        (
                                                            [ID] </span>=> 5<span>
                                                            [PARENT] </span>=> 4<span>
                                                            [NAME] </span>=><span> 儿子
                                                        )

                                                )

                                        )

                                )

                        )

                    [</span>3] => <span>Array</span><span>
                        (
                            [ID] </span>=> 3<span>
                            [PARENT] </span>=> 1<span>
                            [NAME] </span>=><span> 叔伯
                        )

                )

        )

    [</span>2] => <span>Array</span><span>
        (
            [ID] </span>=> 2<span>
            [PARENT] </span>=> 1<span>
            [NAME] </span>=><span> 父亲
            [child] </span>=> <span>Array</span><span>
                (
                    [</span>4] => <span>Array</span><span>
                        (
                            [ID] </span>=> 4<span>
                            [PARENT] </span>=> 2<span>
                            [NAME] </span>=><span> 自己
                            [child] </span>=> <span>Array</span><span>
                                (
                                    [</span>5] => <span>Array</span><span>
                                        (
                                            [ID] </span>=> 5<span>
                                            [PARENT] </span>=> 4<span>
                                            [NAME] </span>=><span> 儿子
                                        )

                                )

                        )

                )

        )

    [</span>3] => <span>Array</span><span>
        (
            [ID] </span>=> 3<span>
            [PARENT] </span>=> 1<span>
            [NAME] </span>=><span> 叔伯
        )

    [</span>4] => <span>Array</span><span>
        (
            [ID] </span>=> 4<span>
            [PARENT] </span>=> 2<span>
            [NAME] </span>=><span> 自己
            [child] </span>=> <span>Array</span><span>
                (
                    [</span>5] => <span>Array</span><span>
                        (
                            [ID] </span>=> 5<span>
                            [PARENT] </span>=> 4<span>
                            [NAME] </span>=><span> 儿子
                        )

                )

        )

    [</span>5] => <span>Array</span><span>
        (
            [ID] </span>=> 5<span>
            [PARENT] </span>=> 4<span>
            [NAME] </span>=><span> 儿子
        )

)</span>
登录后复制

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1135177.htmlTechArticlePHP树-不需要递归,php树递归 /* * * 创建父节点树形数组 * 参数 * $ar 数组,邻接列表方式组织的数据 * $id 数组中作为主键的下标或关联键名...
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板