当有对象时,如何访问数组的项目?

时间:2017-03-10 07:12:44

标签: php arrays object

我有一个这样的数组:

echo "<pre>";
print_r($res_users);

/*
Array
(
    [0] => stdClass Object
        (
            [position] => 1
            [user] => stdClass Object
                (
                    [full_name] => دکوراسیون
                    [is_private] => 1
                    [has_anonymous_profile_picture] => 
                    [byline] => 39.3k followers
                    [profile_pic_url] => https://scontent-frt3-1.cdninstagram.com/t51.2885-19/s150x150/11352072_817936521647202_201395223_a.jpg
                    [pk] => 1480272396
                    [follower_count] => 39326
                    [is_verified] => 
                    [mutual_followers_count] => 0
                    [username] => sajad.sobhi
                )

        )
)

我要做的就是从上面的数组中选择以下网址:

https://scontent-frt3-1.cdninstagram.com/t51.2885-19/s150x150/11352072_817936521647202_201395223_a.jpg

profile_pic_url的值是多少。我怎么能得到它?

注意到这不起作用:

echo $res_users[0]['user']['profile_pic_url'];

2 个答案:

答案 0 :(得分:2)

试试$res_users[0]->user->profile_pic_url。因为$res_users[0]object类型

答案 1 :(得分:1)

通过这种方式尝试 echo $ res_users [0] - &gt; user-&gt; profile_pic_url;