两个不用递归的树形数组操作函数
代码如下:
/**
* 创建父节点树形数组
* 参数
* $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_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,$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,'NAME'=>'叔伯'),
array('ID'=>4,'PARENT'=>2,'NAME'=>'自己'),
array('ID'=>5,'PARENT'=>4,'NAME'=>'儿子'),
);
array('ID'=>1,'PARENT'=>0,'NAME'=>'祖父'),
array('ID'=>2,'PARENT'=>1,'NAME'=>'父亲'),
array('ID'=>3,'NAME'=>'叔伯'),
array('ID'=>4,'PARENT'=>2,'NAME'=>'自己'),
array('ID'=>5,'PARENT'=>4,'NAME'=>'儿子'),
);
$p = find_parent($data,'ID','PARENT');
$c = find_child($data,'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] => 儿子
))
(
[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] => 儿子
))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。