gpt4 book ai didi

php - 值未分配给变量

转载 作者:行者123 更新时间:2023-12-04 05:57:54 26 4
gpt4 key购买 nike

我有一个类别数组,其中 id 是类别的 id,parent 表示类别的父 id(id 0 表示最顶部的父节点),value 是数组的标题。路径最初设置为类别的 id。数组如下:

Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] => 1
)

[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] => 2
)

[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] => 3
)

[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] => 4
)

[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] => 5
)

[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] => 6
)

[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] => 7
)
);

我想在数组中生成类别的路径。所以输出应该是
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] => 1
)

[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] => 2
)

[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] => 1,3
)

[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] => 1,4
)

[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] => 1,5
)

[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] => 2,6
)

[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] => 2,7
)
);

我写了以下函数。
   function findparent($id,$path){
global $categories;
global $catcnt;

if($id==0){
echo $path."<br />"; //this outputs path currently
return $path;
}
for($i=0;$i<$catcnt;$i++){

if($id==$categories[$i]['id']){
$path=$id.",".$path;
findparent($categories[$i]['parent'],$path);
}
}
}

for($i=0;$i<count($categories);$i++){
$categories[$i]['path']=(string)findparent($categories[$i]['parent'],$categories[$i]['id']); //this doesnt assign it currectly

}

输出是:
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] =>
)

[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] =>
)

[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] =>
)

[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] =>
)

[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] =>
)

[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] =>
)

[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] =>
)
);

我哪里错了?

最佳答案

findparent仅在 id 时返回为零。

在递归 findparent 之前,您需要第二个 return 语句。称呼。

关于php - 值未分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9295714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com