在循环中读取数组

时间:2015-01-15 15:55:08

标签: php arrays

我正在尝试读取数组并回显数组中的一个变量,如下所示:

foreach($result->http_response_body as $obj)    {
echo $obj->items['address'];
}

当我打印它时,数组看起来像这样:

    stdClass Object
(
[http_response_body] => stdClass Object
    (
        [total_count] => 1322
        [items] => Array
            (
                [0] => stdClass Object
                    (
                        [created_at] => Thu, 15 Jan 2015 08:35:06 GMT
                        [tag] => *
                        [id] => 54b77bba55edfbe50e662d3a
                        [address] => xxxx@xxxx.xx
                    )

                [1] => stdClass Object
                    (
                        [created_at] => Thu, 15 Jan 2015 08:34:40 GMT
                        [tag] => *
                        [id] => 54b77ba055edfbe50e662cf6
                        [address] => xxxx@xxxx.xx
                    )

                [2] => stdClass Object
                    (
                        [created_at] => Thu, 15 Jan 2015 08:31:56 GMT
                        [tag] => *
                        [id] => 54b77afc55edfbe50e662b0d
                        [address] => xxxx@xxxx.xx
                    )

但是在我的剧本中我会发出这样的信息:

注意:尝试在第27行的/home/xxxxxxxx/domains/xxxxxxx.xx/public_html/MailGun/watchUnsubscribes.php中获取非对象的属性

我做错了什么?

2 个答案:

答案 0 :(得分:3)

您需要将代码更改为:

foreach ($result->http_response_body->items as $obj) {
    echo $obj->address;
}

答案 1 :(得分:-2)

请改为:

foreach($result->http_response_body->items as $obj)    {
echo (string)$obj->address;
}