循环遍历这个数组结构

时间:2014-12-16 01:51:45

标签: php arrays

如何在数组的以下结构中获得children的值?

Array
(
    [post_id] => 2773
    [children] => Array
    (
    )
)

我尝试了这个,但它不起作用:

foreach ($array as $key => $value) {
    print_r($value['children']);
}

它根本没有返回任何内容。

3 个答案:

答案 0 :(得分:1)

您根本不需要进行任何循环。就这样做:

print_r($array['children']);

答案 1 :(得分:1)

您的示例没有返回任何内容,因为children数组为空。

如果您的数组看起来像这样(示例):

Array
(
    [post_id] => 2773
    [children] => Array
    (
         [0] => Item,
         [1] => Item,
    )
)

然后你的print_r($array['children'])会返回一些内容。

或者,你可以这样做:

foreach($array['children'] as $item) {
    echo $item ."<br />";
}

如果数组不是空的

答案 2 :(得分:-1)

您可以使用var_dump($array)代替它,它会为您提供更详细的信息和内部数组。