我有多维数组。它可以延伸至N级。我只想收集名字。从孩子到父母的姓名。例如,我需要像这样的输出
name => “Sport / Algemeen / techn.sp.ondgd / ondergoed dames / Sport bh”
。等级可以增加到N个数字。
[categories] => Array
(
[0] => Array
(
[@id] => 1
[name] => 78057 | Sport bh
[parent] => Array
(
[@id] => 2
[name] => 7805 | ondergoed dames
[parent] => Array
(
[@id] => 3
[name] => 780 | techn.sp.ondgd
[parent] => Array
(
[@id] => 4
[name] => Algemeen
[parent] => Array
(
[@id] => 5
[name] => Sport
)
)
)
)
)
)
答案 0 :(得分:1)
就像那样(尚未测试)
function namify($arr) {
foreach($arr as $cat) {
if(!empty($cat['parent']) && is_array($cat['parent'])) {
return $cat['name'].namify($cat['parent']);
} else {
return $cat['name'];
}
}
}
echo namify($categories);
答案 1 :(得分:1)
$arrPush = array();
function iterator($arr){
global $arrPush;
foreach($arr as $key => $val){
if(is_array($val))
iterator($val);
if($key == "name"){
$str = explode("|", $val);
$arrPush[] = isset($str[1])?$str[1]:$str[0];
}
//filter the $key and $val here and do what you want
}
}
iterator($sweet);
$str = implode("/", array_reverse($arrPush));
echo $str;
Sport / Algemeen / techn.sp.ondgd / ondergoed dames / Sport bh