Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize hierarchical processing in infinite hierarchical data of PHP array

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

小编给大家分享一下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]];

$t[$k]['reference'] = true;

}

}

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];

$t[$k]['reference'] = true;

}

}

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');

上面两种方法是将所有节点按id平摊到一个数组中,然后找到他们的 parent 或 children ,通过引用将 平摊的元素挂接到 parent 、children 下,

但被引用的元素依然存在于平摊的数组中,因此,在实际应用时,最好标记那些被引用的元素,以避免以他们为根开始遍历,导致重复。

复制代码 代码如下:

foreach ($p as $key => $item) {

if($item['reference']) continue;

print_r($item);

}

foreach ($c as $key => $item) {

if($item['reference']) continue;

print_r($item);

}

递归法,PHP 数组元素被删除后,数组游标会归零,因此在遍历过程中一些已经找到 "归宿" 的元素也不得不留在数组中,无法缩减后继元素的搜索范围:

复制代码 代码如下:

$mylist = array(array( 'parent_id'=>0,'id'=>1),

array( 'parent_id'=>0,'id'=>2),

array( 'parent_id'=>0,'id'=>3),

array( 'parent_id'=>2,'id'=>4),

array( 'parent_id'=>2,'id'=>5),

array( 'parent_id'=>3,'id'=>6),

array( 'parent_id'=>3,'id'=>7),

array( 'parent_id'=>4,'id'=>8),

array( 'parent_id'=>5,'id'=>9),

array( 'parent_id'=>5,'id'=>10)

);

function _findChildren($list, $p_id){ //数据层级化,

$r = array();

foreach($list as $id=>$item){

if($item['parent_id'] == $p_id) {

$length = count($r);

$r[$length] = $item;

if($t = $this->_findChildren($list, $item['id']) ){

$r[$length]['children'] = $t;

}

}

}

return $r;

}

print_r(_findChildren($mylist, 0));

以上是"PHP数组无限分级数据中如何实现层级化处理"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report