[英]recursive loop to get values from php array
I search a Solution to get all values from [BrowseNodeId]
and [Name]
from all [Ancestors]
in a new array called categories. 我搜索一个解决方案,以从名为“类别”的新数组中的所有[Ancestors]
[BrowseNodeId]
和[Name]
获取所有值。 I think it must be a recursive loop, but how? 我认为这一定是一个递归循环,但是如何?
Array (
[BrowseNodeId] => 343505011
[Name] => Kinderzimmer
[Ancestors] => Array (
[BrowseNode] => Array (
[BrowseNodeId] => 3517801
[Name] => Möbel
[Ancestors] => Array (
[BrowseNode] => Array (
[BrowseNodeId] => 3312261
[Name] => Möbel & Wohnaccessoires
[Ancestors] => Array (
[BrowseNode] => Array (
[BrowseNodeId] => 3169011
[Name] => Kategorien
[IsCategoryRoot] => 1
[Ancestors] => Array (
[BrowseNode] => Array (
[BrowseNodeId] => 3167641
[Name] => Küche & Haushalt
)
)
)
)
)
)
)
)
)
Try something like this 试试这个
function getAllInfo($inputArray, $outputArray = array()) {
$outputArray[] = array(
'BrowseNodeId' => $inputArray['BrowseNodeId'],
'Name' => $inputArray['Name']
);
if(isset($inputArray['Ancestors']['BrowseNode'])) {
return getAllInfo($inputArray['Ancestors']['BrowseNode'], $outputArray);
}
return $outputArray;
}
See it in action here (with the test array you provided): http://3v4l.org/jd7hY 在此处(使用您提供的测试阵列)对其进行操作:http: //3v4l.org/jd7hY
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.