PHP - 访问多维数组时出现问题


PHP - problems accessing multidimensional array

在使用

FirePHP查看我正在使用的内容时,我从这个多维(?)数组中获取正确的数据时遇到问题:

array(
    ['day'] => 'Wed'
    ['is_used'] => 1
    [0] =>
    array(
        ['day'] =>
        'Wed'
        ['title'] =>
        'onsdag denna veckan 2'
        ['content'] =>
        ['price'] =>
    )
    [1] =>
    array(
        ['day'] =>
        'Wed'
        ['title'] =>
        'onsdagslunchen'
        ['content'] =>
        ['price'] =>
        123123
    )
)

我想在数组中使用 0, 1 数组,如果这有任何意义的话......?还是这个阵列坏了?

当我尝试时

foreach ($foo as $bar){
    echo $bar
}

我得到了 4 个结果,天、is_used 和 0、1 个数组。

代码正确返回值。它返回$foo数组的第一个子级,它们是:

day
is_used
0
1

dayis_used 是字符串,但由于 01 是数组,因此您需要再次循环访问它们以获取它们的值:

foreach ($foo as $fooKey=>$bar){
    if(is_array($bar))
        // cycle through the array
        foreach ($bar as $key=>$value)
            // Echo out the string of the array
            echo "$fooKey $key = $value<br />";
        }
    } else {
        // Echo out the string
       echo "$fooKey = $bar<br />";
    }
}

这应该呼应以下内容:

day = Wed
is_used = 1
0 day = Wed
0 title = onsdag denna veckan 2
0 content =
0 price =
1 day = Wed
1 title = onsdagslunchen
1 content =
1 price = 123123

也许这就是你想要的:

foreach ($my_array as $key=>$val) {
    if (is_numeric($key) {
        // do something with $val (where $val is an array in the example)
    }
}