我不善于递归从来没有过多地处理它,但最近我被要求。
我不得不以递归方式生成多维数组,并在此处找到了一种方法:Recursive function to generate multidimensional array from database result
但是我现在还需要计算每个父母所拥有的孩子数量,我不知道如何采用以下功能来实现这一目标?
function generateCorrectArray(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $key => $element) {
if ($element['parent'] == $parentId) {
$children = generateCorrectArray($elements, $element['category_id']);
if ($children) {
$element['children'] = $children;
}
$branch[$element['category_id']] = $element;
}
}
return $branch;
}
修改
数组我从:
开始$someArray = array(
array(
"id"=> 1,
"name"=> "somename1",
"parent"=> 0,
"childrenCount" => 0,
"children" => 0
),
array(
"id"=> 53,
"name"=> "somename2",
"parent"=> 1,
"childrenCount" => 0,
"children" => 0
),
array(
"id"=> 921,
"name"=> "somename3",
"parent"=> 53,
"childrenCount" => 0,
"children" => 0
)
当前功能结果
$someArray = array(
array(
"id"=> 1,
"name"=> "somename1",
"parent"=> 0,
"children" => array(
array(
"id"=> 53,
"name"=> "somename2",
"parent"=> 1,
"children" => array(
array(
"id"=> 921,
"name"=> "somename3",
"parent"=> 53,
"children" => array(
)
)
)
)
)
)
我希望每个嵌套的孩子都有一个计数。
目标
$someArray = array(
array(
"id"=> 1,
"name"=> "somename1",
"parent"=> 0,
"childrenCount"=> 2,
"children" => array(
array(
"id"=> 53,
"name"=> "somename2",
"parent"=> 1,
"childrenCount"=> 1,
"children" => array(
array(
"id"=> 921,
"name"=> "somename3",
"parent"=> 53,
"childrenCount"=> 0,
"children" => array(
)
)
)
)
)
)
先谢谢。
答案 0 :(得分:0)
试试这个:
function generateCorrectArray(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $key => $element) {
if ($element['parent'] == $parentId) {
$children = generateCorrectArray($elements, $element['category_id']);
if ($children) {
$element['children'] = $children;
if(array_key_exists('childrenCount', $element)) {
$element['childrenCount'] = $element['childrenCount'] + 1;
} else {
$element['childrenCount'] = 1;
}
}
$branch[$element['category_id']] = $element;
}
}
return $branch;
}
答案 1 :(得分:0)
/*
Gets reversed array,
Returns multidimensional tree array.
*/
function buildTree($parts) {
if (count($parts) == 1) {
return $parts[0];
}
$last_item = array_pop($parts);
$last_item['childrenCount'] = count($parts);
$last_item['children'] = buildTree($parts);
return $last_item;
}
经测试&工作:))
示例:
$parts = array(
array('1','2','3',5),
array('3','8','3',1),
array('1', 5,'2','3'),
array('D','2','3',5),
array('A','2','3',5)
);
var_dump(buildTree(array_reverse($parts)));
结果:
array(6) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> int(5) ["childrenCount"]=> int(4) ["children"]=> array(6) { [0]=> string(1) "3" [1]=> string(1) "8" [2]=> string(1) "3" [3]=> int(1) ["childrenCount"]=> int(3) ["children"]=> array(6) { [0]=> string(1) "1" [1]=> int(5) [2]=> string(1) "2" [3]=> string(1) "3" ["childrenCount"]=> int(2) ["children"]=> array(6) { [0]=> string(1) "D" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> int(5) ["childrenCount"]=> int(1) ["children"]=> array(4) { [0]=> string(1) "A" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> int(5) } } } } }
答案 2 :(得分:0)
使用递归数组和记录集的一个问题是,除非您只是显示结果,否则最终会重写大量代码以便能够操作数据。 例如,如果删除子项,则必须遍历整个数组以更新树的其余部分,或者编写另一个函数以展平数组树以迭代并检索节点属性。 如果您需要查找特定节点的深度或根节点而不仅仅是子节点数,会发生什么?
尝试一个对象来存储你的树,它可以跟踪继承并执行数组不能的功能。这将使得在尝试计算递归时以及如果您需要为其添加功能时,更容易使用记录集并且更省时。
以下是我在编写ORM之前使用过的示例。 http://phpfiddle.org/lite/code/u07-fnd
我将它设计为能够迭代整个树(平面数组),单个节点或节点的子节点。我还需要子计数,节点在树中的距离(深度),以及能够找到根节点。希望它有所帮助。
单击右上角的“运行”以查看层次结构的生成方式以及有关如何使用它的一些示例的结果。