php 树排序,PHP数组树排序

我有以下数组.它具有与该ID对应的父ID.我设法创建一个函数来对该数组进行排序并将其转换为树形数组.我的问题是,如果父母追随孩子,有时它可能无法正常工作.

那么,如何将下面的数组变成不需要先排序的树呢?

[0] => Array

(

[menu] =>

[parent] => 0

[id] => 1

)

,

[1] => Array

(

[menu] =>

[parent] =>

[id] => 2

)

,

[2] => Array

(

[menu] =>

[parent] => 1

[id] => 3

)

,

[3] => Array

(

[menu] =>

[parent] => 1

[id] => 4

)

,

[4] => Array

(

[menu] =>

[parent] => 4

[id] => 5

)

我有此功能无法正常工作:

function page_tree($rows) {

if(!is_array($rows) || empty($rows) ){

return false;

}

// $rows = array(); //stores all the database rows that are to be converted into a tree

$tree = array(); //stores the tree

$tree_index = array(); //an array used to quickly find nodes in the tree

$id_column = "id"; //The column that contains the id of each node

$parent_column = "parent"; //The column that contains the id of each node's parent

$text_column = "title"; //The column to display when printing the tree to html

//build the tree - this will complete in a single pass if no parents are defined after children

// vp(count($rows) );die();

// while(count($rows) > 0){

foreach($rows as $row_id => $row){

$row_id = $row['id'];

if($row[$parent_column]){

if((!array_key_exists($row[$parent_column], $rows)) and (!array_key_exists($row[$parent_column], $tree_index))){

unset($rows[$row_id]);

}

else{

if(array_key_exists($row[$parent_column], $tree_index)){

$parent = & $tree_index[$row[$parent_column]];

$parent['children'][$row_id] =$row;

$parent['children'][$row_id]["children"] = array();

$tree_index[$row_id] = & $parent['children'][$row_id];

unset($rows[$row_id]);

}

}

}

else{

$tree[$row_id] = $row;

$tree[$row_id]["children"] = array();

$tree_index[$row_id] = & $tree[$row_id];

unset($rows[$row_id]);

}

}

// }

return $tree;

}

请注意:如果父级是(空)(=”;;),则表示它是根.

解决方法:

诀窍是保留对树中所有节点的引用的一种索引(在下面称为$all).下面的示例将仍需要处理的节点添加到名为$dangling的数组中,并将最终输出添加到$output数组中.

// Test input

$input = array(

array( 'menu' => 'A', 'parent' => 2, 'id' => 4),

array( 'menu' => 'B', 'parent' => 1, 'id' => 3),

array( 'menu' => 'C', 'parent' => 2, 'id' => 1),

array( 'menu' => 'D', 'parent' => '', 'id' => 2)

);

$output = array();

$all = array();

$dangling = array();

// Initialize arrays

foreach ($input as $entry) {

$entry['children'] = array();

$id = $entry['id'];

// If this is a top-level node, add it to the output immediately

if ($entry['parent'] == '') {

$all[$id] = $entry;

$output[] =& $all[$id];

// If this isn't a top-level node, we have to process it later

} else {

$dangling[$id] = $entry;

}

}

// Process all 'dangling' nodes

while (count($dangling) > 0) {

foreach($dangling as $entry) {

$id = $entry['id'];

$pid = $entry['parent'];

// If the parent has already been added to the output, it's

// safe to add this node too

if (isset($all[$pid])) {

$all[$id] = $entry;

$all[$pid]['children'][] =& $all[$id];

unset($dangling[$entry['id']]);

}

}

}

print_r($output);

请注意,如果您输入的数据不正确,这将导致严重错误(例如,父项值无效的项目将导致无限循环).

标签:php,arrays

来源: https://codeday.me/bug/20191013/1911176.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值