-1

从我使用 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 个结果,day、is_used 和 0、1 数组。

4

2 回答 2

2

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

day
is_used
0
1

dayandis_used是字符串,但是因为0and1是数组,所以您需要再次循环遍历它们以获取它们的值:

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
于 2012-08-15T08:37:41.757 回答
0

也许这就是你想要的:

foreach ($my_array as $key=>$val) {
    if (is_numeric($key) {
        // do something with $val (where $val is an array in the example)
    }
}
于 2012-08-15T08:36:26.543 回答