温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP中实现无限分级的方式有哪些

发布时间:2021-01-20 15:11:01 来源:亿速云 阅读:141 作者:Leah 栏目:开发技术

PHP中实现无限分级的方式有哪些?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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

复制代码 代码如下:


/**
 * 创建父节点树形数组
 * 参数
 * $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中实现无限分级的方式有哪些问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI