遍历此数组结构


Loop through this structure of array

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

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

我试过这个,但没有用:

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

它什么也没返回。

你根本不需要做任何循环。只需这样做:

print_r($array['children']);

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

如果您的数组如下所示(示例):

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

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

或者,您可以这样做:

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

前提是数组不为空

你可以

改用var_dump($array),它将为您提供更详细的信息和内部数组。