php – foreach ruft das stdClass-Objekt ab
天蓬老师
天蓬老师 2017-06-08 11:01:53
0
4
1082

Das Folgende sind die erhaltenen grafischen NewsJSON-Daten. Wie erhält foreach Werte wie Titel, Autor, Digest usw.?

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

欢迎选择我的课程,让我们一起见证您的进步~~

Antworte allen(4)
大家讲道理

个人建议最好将json字符串转换成数组而不是对象,这样便于后续操作,也便于阅读。

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

assoc 当该参数为 TRUE 时,将返回 array 而非 object 。
试着将json_decode()第二个参数置为true。

刘奇

假设上面的对象名为obj

$data = array();
foreach ($obj->item as $item) {
    $data[] = $item->content->new_item[0];
}
var_dump($data);
Peter_Zhu
     $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;
刘奇

好好理解@windfly说的,
访问stdClass Object属性, 用:

$obj->title;

访问数组属性用:

$obj['title'];

显然你取不到, 是用 $obj['title'] 这样的写法写的

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!