Rumah >php教程 >php手册 >不用递归实现php树程序代码

不用递归实现php树程序代码

WBOY
WBOYasal
2016-05-25 16:43:171458semak imbas

本文章介绍了一段关于不用递归实现php树程序代码,实例代码如下:

<?php
header(&#39;Content-type:text/html;charset=utf-8&#39;);
/** 
 * 创建父节点树形数组
 * 参数
 * $ar 数组,邻接列表方式组织的数据
 * $id 数组中作为主键的下标或关联键名
 * $pid 数组中作为父键的下标或关联键名
 * 返回 多维数组
 *
 */
function find_parent($ar, $id = &#39;id&#39;, $pid = &#39;pid&#39;) {
    foreach ($ar as $v) $t[$v[$id]] = $v;
    foreach ($t as $k => $item) {
        if ($item[$pid]) {
            if (!isset($t[$item[$pid]][&#39;parent&#39;][$item[$pid]])) $t[$item[$id]][&#39;parent&#39;][$item[$pid]] = & $t[$item[$pid]];
        }
    }
    return $t;
}
/** 
 * 创建子节点树形数组
 * 参数
 * $ar 数组,邻接列表方式组织的数据
 * $id 数组中作为主键的下标或关联键名
 * $pid 数组中作为父键的下标或关联键名
 * 返回 多维数组
 *
 */
function find_child($ar, $id = &#39;id&#39;, $pid = &#39;pid&#39;) {
    foreach ($ar as $v) $t[$v[$id]] = $v;
    foreach ($t as $k => $item) {
        if ($item[$pid]) {
            $t[$item[$pid]][&#39;child&#39;][$item[$id]] = & $t[$k];
        }
    }
    return $t;
}
$data = array( 
      array(&#39;ID&#39;=>1, &#39;PARENT&#39;=>0, &#39;NAME&#39;=>&#39;祖父&#39;), 
      array(&#39;ID&#39;=>2, &#39;PARENT&#39;=>1, &#39;NAME&#39;=>&#39;父亲&#39;), 
      array(&#39;ID&#39;=>3, &#39;PARENT&#39;=>1, &#39;NAME&#39;=>&#39;叔伯&#39;), 
      array(&#39;ID&#39;=>4, &#39;PARENT&#39;=>2, &#39;NAME&#39;=>&#39;自己&#39;), 
      array(&#39;ID&#39;=>5, &#39;PARENT&#39;=>4, &#39;NAME&#39;=>&#39;儿子&#39;), 
    ); 
$p = find_parent($data, &#39;ID&#39;, &#39;PARENT&#39;);
$c = find_child($data, &#39;ID&#39;, &#39;PARENT&#39;);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
Print_r($c);
echo &#39;
'; /*   结果如下:  Array  (      [1] => Array          (              [ID] => 1              [PARENT] => 0              [NAME] => 祖父              [child] => Array                  (                      [2] => Array                          (                              [ID] => 2                              [PARENT] => 1                              [NAME] => 父亲                              [child] => Array                                  (                                      [4] => Array                                          (                                              [ID] => 4                                              [PARENT] => 2                                              [NAME] => 自己                                              [child] => Array                                                  (                                                      [5] => Array                                                          (                                                              [ID] => 5                                                              [PARENT] => 4                                                              [NAME] => 儿子                                                          )                                                    )                                            )                                    )                            )                        [3] => Array                          (                              [ID] => 3                              [PARENT] => 1                              [NAME] => 叔伯                          )                    )            )        [2] => Array          (              [ID] => 2              [PARENT] => 1              [NAME] => 父亲              [child] => Array                  (                      [4] => Array                          (                              [ID] => 4                              [PARENT] => 2                              [NAME] => 自己                              [child] => Array                                  (                                      [5] => Array                                          (                                              [ID] => 5                                              [PARENT] => 4                                              [NAME] => 儿子                                          )                                    )                            )                    )            )        [3] => Array          (              [ID] => 3              [PARENT] => 1              [NAME] => 叔伯          )        [4] => Array          (              [ID] => 4              [PARENT] => 2              [NAME] => 自己              [child] => Array                  (                      [5] => Array                          (                              [ID] => 5                              [PARENT] => 4                              [NAME] => 儿子                          )                    )            )        [5] => Array          (              [ID] => 5              [PARENT] => 4              [NAME] => 儿子          )    )  */


本文地址:

转载随意,但请附上文章地址:-)

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn