search

Home  >  Q&A  >  body text

foreach get stdClass Object - Stack Overflow

The following is the graphic newsJSON data obtained. How does foreach obtain values ​​such as title, author, digest, etc.?

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
)
天蓬老师天蓬老师2768 days ago1128

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-06-08 11:03:53

    Personally, it is recommended to convert the json string into an array instead of an object, so as to facilitate subsequent operations and readability.

    mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

    assoc When this parameter is TRUE, array will be returned instead of object.
    Try to set the second parameter of json_decode() to true.

    reply
    0
  • 怪我咯

    怪我咯2017-06-08 11:03:53

    Suppose the above object is named obj

    $data = array();
    foreach ($obj->item as $item) {
        $data[] = $item->content->new_item[0];
    }
    var_dump($data);

    reply
    0
  • 天蓬老师

    天蓬老师2017-06-08 11:03:53

         $object = (object) [
            'item' => [
                ['content' => [
                    'news_item' => [
                        'title'  => '标题1',
                        'author' => '作者1'
                        ]
                    ]
                ],
                ['content' => [
                    'news_item' => [
                        'title'  => '标题2',
                        'author' => '作者2'
                        ]
                    ]
                ]]
                
          ];
         
        $result = new stdClass();
        foreach($object->item as $val) {
            $result->title[] = $val['content']['news_item']['title'];
        }
        
        var_dump($result);die;

    reply
    0
  • 怪我咯

    怪我咯2017-06-08 11:03:53

    Understand what @windfly said,
    To access the stdClass Object property, use:

    $obj->title;

    To access array properties use:

    $obj['title'];

    Obviously you can’t get it, it’s written like $obj['title']

    reply
    0
  • Cancelreply