帮助迭代数组PHP

时间:2011-04-11 12:54:42

标签: php arrays

我想返回此数组的'message-> text'部分。你能帮助我吗?我对此有些困难。

Array (
    [name] => new exp
    [created] => Array (
        [date] => 20110304
        [time] => 11:09:27
        [version] => 2.02.6
    )
    [message] => Array (
        [0] => Array (
            [@attributes] => Array (
                [id] => 0 [mbox] => recipe
            )
            [received] => Array (
                [date] => 20090304
                [time] => 13:16:48
            )
            [from] => Array (
                [name] => Andrew Welch
                [email] => <email address removed>
            )
            [to] => Array (
                [name] => <email address removed>
                [email] => <email address removed>
            )
            [messageid] => <email address removed>
            [subject] => aloo paranthas
            [text] => you take potatoes ... to cook.
            [attachment] => Array (
                [name] => part-003.html
                [dir] => page0001/000000
                [bytes] => 864
                [size] => 864.00 B
                [type] => text/html
            )
            [mboxname] => recipe
        )
        [1] => Array (
            ...

1 个答案:

答案 0 :(得分:3)

数组结构如下:

Array (
    ...
    [message] => Array (
        [0] => Array (
            ...
            [text] => you take potatoes ... to cook.
            ...
        )
        [1] => Array (
            ...
        )
        ...
    )
)

所以$data['message']本身就是一个数组:

for($i = 0, $l = count($data['message']); $i < $l ; $i++) {
    echo $data['message'][$i]['text'];
}

我建议您阅读arrays in PHP