php - 递归函数的返回结果
问题描述
我得到了一个当前echo
结果的递归函数。我希望这个函数返回结果并使用foreach
内部标记循环它们。
我知道我应该对每个数组使用某种迭代来获得我想要的结果,但是却失败了。目前我的代码看起来像这样(尝试迭代):
public static function recursiveChildCategory($categories = array(), $depth = 0, $i = 0) {
$ca = [];
// Loop through categories
foreach($categories as $key => $category){
echo str_repeat(" ", $depth);
echo "<a href='".implode('/', $category['breadcrumb'])."'>{$category['title']}</a>";
echo '<br>';
$ca[$i] = [
'id' => $category['id'],
'title' => $category['title'],
];
if(isset($category['child'])) {
// Loop
self::recursiveChildCategory($category['child'], $depth + 1, $i++);
}
}
return $ca;
}
以及函数的传入数组:
Array (
[0] => Array (
[id] => 7
[title] => Deserts
[slug] => deserts
[child] => Array (
[0] => Array (
[id] => 8
[title] => Space
[slug] => space
[child] => Array (
[0] => Array (
[id] =>
[title] =>
[slug] =>
[child] => Array ( )
)
)
)
)
)
)
目前它只返回第一级子类别“沙漠”,但没有关于“空间”。
作为期望的结果,我希望函数返回具有$depth
多个子类别的所有类别和无限路径(与当前所做的工作相同echo
)。
谢谢指教
解决方案
试试这个,告诉我它是否是你要找的:
我的测试数组:
$array = [
0 => [
"id" => 7,
"title" => "Deserts",
"slug" => "deserts",
"child" => [
0 => [
"id" => 8,
"title" => "Space",
"slug" => "space",
"child" => [
0 => [
"id" => 9,
"title" => "Test",
"slug" => "test"
]
]
]
]
]
];
递归函数:
function recursiveChildCategory($categories, $depth = 0, $ca = []) {
// Loop through categories
foreach($categories as $key => $category){
$ca[$depth] = [
'id' => $category['id'],
'title' => $category['title'],
];
if(isset($category['child'])) {
// Loop
$ca = recursiveChildCategory($category['child'], $depth + 1, $ca);
} else {
break;
}
}
return $ca;
}
现在,如何使用它:
$test = recursiveChildCategory($array);
var_dump($test);
这是输出:
array(3) {
[0]=>
array(2) {
["id"]=>
int(7)
["title"]=>
string(7) "Deserts"
}
[1]=>
array(2) {
["id"]=>
int(8)
["title"]=>
string(5) "Space"
}
[2]=>
array(2) {
["id"]=>
int(9)
["title"]=>
string(4) "Test"
}
}
这是一个测试它的链接: http: //sandbox.onlinephpfunctions.com/
编辑:我做了一些修改,因为在 OP 示例数组中,第一个“深度”中可以有多个结果,这是“新”解决方案:
用于测试的数组:
$array = [
"0" =>
[ "id" => 3, "title" => "Subcat", "slug" => "subcat", "child" =>
[ "0" =>
[ "id" => 5, "title" => "Subsubcat2", "slug" => "subcat2", "child" =>
[ "0" =>
[ "id" => "", "title" => "", "slug" =>"", "breadcrumb" => [ "0" => "homeworld", "1" => "cat", "2" => "subcat", "3" => "subcat2" ], "child" => [ ] ]
]
]
]
],
"1" =>
[ "id" => 4, "title" => "Kalahari", "slug" => "kalahari", "child" =>
[ "0" => [ "id" => 7, "title" => "deserts", "slug" => "deserts", "child" =>
[ "0" =>
[ "id" => 8, "title" => "Ural", "slug" => "ural", "child" =>
[ "0" => [ "id" =>"", "title" =>"", "slug" =>"", "child" => [ ] ] ]
]
]
]
]
]
];
功能:我只是添加$ca[$depth][]
而不是$ca[$depth]
function recursiveChildCategory($categories, $depth = 0, $ca = []) {
// Loop through categories
foreach($categories as $key => $category){
$ca[$depth][] = [
'id' => $category['id'],
'title' => $category['title'],
];
if(isset($category['child'])) {
// Loop
$ca = recursiveChildCategory($category['child'], $depth + 1, $ca);
} else {
break;
}
}
return $ca;
}
现在结果:
$test = recursiveChildCategory($array);
foreach ($test as $depth => $c) {
echo "depth : ".$depth."\n";
foreach ($c as $result) {
echo "Title : ".$result['title']."\n";
}
echo "=============\n";
}
输出是:
depth : 0
Title : Subcat
Title : Kalahari
=============
depth : 1
Title : Subsubcat2
Title : deserts
=============
depth : 2
Title :
Title : Ural
=============
depth : 3
Title :
=============
在这里测试:链接
推荐阅读
- imagemagick - 如何在带有 imagemagick 的脚本中使用 pango
- amazon-web-services - 从本地机器连接到 aws 远程 mariadb 服务器
- java - Flink SQL:在 GROUP BY 查询的结果中重复分组键
- wikipedia - 如何发送维基百科页面的地址而不是完整页面?
- eclipse - 黄瓜硒 Java Eclipse
- javascript - 正则表达式匹配不被引号包围的 URL
- dialogflow-es - DialogFlow V2 文本查询配额
- listview - 如何为选定的 ListItem onTap 着色
- javascript - window.location.href 未在 Edge 中显示内容
- java - 缺少 Java Struts 2.5.17 LocalizedTextUtil 类