如何使用foreach循环来循环图像


how to loop images using foreach loop

我想使用foreach循环循环来自Facebook invitable_friends API的图像

$beranda = json_decode(get_html("https://graph.facebook.com/$user->id/invitable_friends?fields=id,picture,name&limit=7&access_token=$accessToken"))->data;
foreach($beranda as $friendsid){

echo  '<img src="'.$friendsid->picture.'">';
 }

面对这个错误

Catchable fatal error: Object of class stdClass could not be converted to string in

Thia是$beranda 的结构

Array
  (
  [0] => stdClass Object
    (
        [id] => AVkUphJYSPksSJ47kLM8PGm8cOAjlNLG5NWUMp5IhdSCWkziShWsSVkRdVx1Z73jeu7B73bRzYnecUqD27V3xO36Uh27LD16mVZlRKGKgDLZIQ
        [picture] => stdClass Object
            (
                [data] => stdClass Object
                    (
                        [is_silhouette] => 
                        [url] => https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/13731636_1671465926510554_2031112688215377192_n.jpg?oh=5a85a07de9ab4011e17378a38bac941c&oe=5831B7E6
                    )
            )
        [name] => Sam Saifi
    )

$friendsid->picture 的无功转储

       object(stdClass)#38 (1) {
           ["data"]=>
             object(stdClass)#39 (2) {
              ["is_silhouette"]=>
                 bool(false)
                    ["url"]=>
              string(145) "https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/10155363_1386729058279575_5046516924942639505_n.jpg?oh=f55ea91f39815a7fb9661e65899d0a02&oe=581B8AE2"

}}

根据打印结果,您需要更改行。。。

echo  '<img src="'.$friendsid->picture.'">';

echo '<img src="' . $friendsid->picture->data->url . '">';

因为您正在调用一个参数[url],在一个对象[data]内,在另一个对象[picture]内,另一个父对象内。我会推荐一个关于面向对象php的训练营课程,无论如何,很高兴它能起作用!