如何使用foreach获取嵌套数组的键值?

时间:2018-01-06 09:41:07

标签: php arrays

enter image description here

我的代码如下:

foreach ($tmpArray as $keys => $values) {
        if(is_array($values)) {
            foreach ($values as $key => $value) {
                if($key == "category_name"){
                    echo $value['category_name'];
                }
            }
        } else {
            if($keys == "category_name"){
                echo $values;
            }
        }
    }

输出:Vasıta-> Bmw X Serisi 我想这样做:Vasıta-> Bmw X Serisi-> Deneme

如何使用foreach循环打印category_name?

1 个答案:

答案 0 :(得分:0)

if (is_array($tmpArray)) {
echo isset($tmpArray['category_name']) ? $tmpArray['category_name'] . '->' : '';
foreach ($tmpArray as $key1 => $value1) {
    if (is_array($value1)) {
        echo isset($value1['category_name']) ? $value1['category_name'] . '->' : '';
        foreach ($value1 as $key2 => $value2) {
            if (is_array($value2)) {
                echo isset($value2['category_name']) ? $value2['category_name'] : '';
            }
        }
    }
}
}