【问题标题】:How can I access array's item when there is an object in it? [duplicate]当其中有对象时如何访问数组的项目? [复制]
【发布时间】:2017-03-10 07:12:43
【问题描述】:

我有一个这样的数组:

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
                )

        )
)

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

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'];

【问题讨论】:

  • 反对者请发表评论并解释我的问题有什么问题?
  • $res_users[0]-&gt;user-&gt;profile_pic_url; 确保你知道数组和对象之间的区别

标签: php arrays object


【解决方案1】:

试试$res_users[0]-&gt;user-&gt;profile_pic_url。因为$res_users[0]object 类型

【讨论】:

  • 你是对的@Saumini
  • 谢谢 .. 点赞。我可以将那个内部对象转换为一个数组吗?
  • @stack 以下会将其转换为数组。 $user = json_decode(json_encode($res_users[0]), true);。然后像$user['user']['profile_pic_url'] 一样使用它
【解决方案2】:

这样试试 echo $res_users[0]->user->profile_pic_url;

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 2023-03-13
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 2020-01-19
    相关资源
    最近更新 更多