以下是取得到的圖文newsJSON數據,請問foreach如何取得到如title、author、digest等這些值呢?
stdClass Object
(
[item] => Array
(
[0] => stdClass Object
(
[media_id] => media_id1
[content] => stdClass Object
(
[news_item] => Array
(
[0] => stdClass Object
(
[title] => 标题1
[author] => 作者1
[digest] => 摘要1
[content] => 内容1
[content_source_url] =>
[thumb_media_id] => media_id1
[show_cover_pic] => 0
[url] => http://mp.weixin.qq.com/
[thumb_url] => http://mmbiz.qpic.cn/mmbiz/jpeg
[need_open_comment] => 0
[only_fans_can_comment] => 0
)
)
[create_time] => 1438240064
[update_time] => 1438307092
)
[update_time] => 1438307092
)
[1] => stdClass Object
(
[media_id] => media_id2
[content] => stdClass Object
(
[news_item] => Array
(
[0] => stdClass Object
(
[title] => 标题2
[author] => 作者2
[digest] => 摘要2
[content] => 内容2
[content_source_url] =>
[thumb_media_id] => media_id2
[show_cover_pic] => 0
[url] => http://mp.weixin.qq.com/
[thumb_url] => http://mmbiz.qpic.cn/mmbiz/jpeg
[need_open_comment] => 0
[only_fans_can_comment] => 0
)
)
[create_time] => 1438156103
[update_time] => 1444380718
)
[update_time] => 1444380718
)
)
[total_count] => 5
[item_count] => 4
)
大家讲道理2017-06-08 11:03:53
個人建議最好將json字串轉換成數組而不是對象,這樣便於後續操作,也方便閱讀。
mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
assoc 當此參數為 TRUE 時,將傳回 array 而非 object 。
試著將json_decode()第二個參數置為true。
怪我咯2017-06-08 11:03:53
假設上面的物件名稱為obj
$data = array();
foreach ($obj->item as $item) {
$data[] = $item->content->new_item[0];
}
var_dump($data);
怪我咯2017-06-08 11:03:53
好好理解@windfly說的,
訪問stdClass Object屬性, 用:
$obj->title;
存取數組屬性用:
$obj['title'];
顯然你取不到, 是用 $obj['title'] 這樣的寫法寫的