2015-07-16 02:31


  • foreach
  • loops
  • php
  • arrays

I am very basic new php learner, i having difficulty to get nested array value, here is my json result:

stdClass Object
[title] => Aao Raja - Gabbar Is Back | Chitrangada Singh
[link] => stdClass Object
        [22] => Array
                [0] =>,expire,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,nh,pl,ratebypass,source,upn&key=cms1&sver=3&expire=1437035009&upn=9lTw9Popb18&ratebypass=yes&source=youtube&lmt=1432539432699196&fexp=901816%2C9407809%2C9408142%2C9408420%2C9408710%2C9409172%2C9412774%2C9412846%2C9413149%2C9415664%2C9415958%2C9416126%2C9416370%2C9416656&ipbits=0&signature=3547894526817B37774A7838F8B68493CDD62101.3F143C74D76E8705800445A4CD4476C4F8BCD988&cms_redirect=yes&mm=31&mn=sn-aigllnsk&ms=au&mt=1437013301&mv=m&nh=IgpwcjAzLmxocjE0KgkxMjcuMC4wLjE&utmg=ytap1
                [1] => 
                [2] => hd720

        [43] => Array
                [0] =>,expire,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,nh,pl,ratebypass,source,upn&key=cms1&sver=3&expire=1437035009&upn=9lTw9Popb18&ratebypass=yes&source=youtube&lmt=1428933984759484&fexp=901816%2C9407809%2C9408142%2C9408420%2C9408710%2C9409172%2C9412774%2C9412846%2C9413149%2C9415664%2C9415958%2C9416126%2C9416370%2C9416656&ipbits=0&signature=266C126464ECDB4CC0FF076CD41F07BCC4DA7E34.08D9F13B7BF7D92FD1E1963336CC7FB8F19FE899&cms_redirect=yes&mm=31&mn=sn-aigllnsk&ms=au&mt=1437013301&mv=m&nh=IgpwcjAzLmxocjE0KgkxMjcuMC4wLjE&utmg=ytap1
                [1] => 
                [2] => medium

I can access the Title, but can't access the Link urls:

echo $title = $json->{'title'};

echo $link = $json->{'link'}->{'22'}->{'0'};

How can access the specific link array 22

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • donglin1192 donglin1192 6年前

    This echo $title = $json->{'title'}; works because you are accessing an object's property and using -> is the correct way. In this case $json->{'link'}->{'22'}->{'0'} you are trying to access an array item instead an object's property, because $json->{'link'}->{'22'} is an array and not an object. In this case, you should access it in this way: $json->{'link'}->{'22'}[0]. In order to avoid this kind of issues and, when you decode your JSON to a PHP object, you can pass true as a second parameter to the function json_decode and that will convert the whole object into an array. That way, you don't need to worry about accessing elements as object's attributes, you can access them, always, as array items. So, in this case, it would be: $json["link"]["22"][0].

    点赞 评论 复制链接分享
  • dsa122870 dsa122870 6年前

    I suppose you use json_decode() function. Do you know that you can get an array instead of StdClass Object? So, you can use.

    $php_array = json_decode($json_string, true);
    点赞 评论 复制链接分享
  • dongqie7806 dongqie7806 6年前

    You're confusing the way you access objects and arrays.

    Getting the title is correct via $json->title, but the link should be $json->link->{'22'}[0] - a mixture of objects and arrays.

    FYI the {'name'} notation is the same as name - only required when you are including variables in your object name e.g. {$someVar . 'name'}

    点赞 评论 复制链接分享