网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )

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

复制代码 代码如下:
/**
 * 创建父节点树形数组
 * 参数
 * $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] => 儿子
        )

)

上一篇:PHP中批量生成静态html(命令行下运行PHP)
下一篇:利用php下载xls文件(自己动手写的)
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?