从多层多维数组中提取名称



我有多维数组。它可以扩展到N级。我只想收集名字。从孩子到父母的名字。例如,我需要这样的输出

name => "Sport/Algemeen/technology .sp. "ondgd/ondergodames/Sport"

。级别可以增加到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
                                                    )
                                            )
                                    )
                            )
                    )
            )
$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;

体育/刊登/techn.sp。ondgd/ondergodames/Sport bh

就像这样(还没有测试过)

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);

相关内容

  • 没有找到相关文章

最新更新