欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP不用递归实现无限分级的例子分享

程序员文章站 2023-11-24 17:26:28
两个不用递归的树形数组操作函数 复制代码 代码如下:/** * 创建父节点树形数组 * 参数 * $ar 数组,邻接列表方式组织的数据&nb...

两个不用递归的树形数组操作函数

复制代码 代码如下:

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


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

使用例子:

复制代码 代码如下:

$data = array(
  array('id'=>1, 'parent'=>0, 'name'=>'祖父'),
  array('id'=>2, 'parent'=>1, 'name'=>'父亲'),
  array('id'=>3, 'parent'=>1, 'name'=>'叔伯'),
  array('id'=>4, 'parent'=>2, 'name'=>'自己'),
  array('id'=>5, 'parent'=>4, 'name'=>'儿子'),
);

$p = find_parent($data, 'id', 'parent');
$c = find_child($data, 'id', 'parent');


执行效果:

复制代码 代码如下:

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] => 儿子
        )

)