简体   繁体   中英

How to access a JSON element which has ':' in its name using PHP

I am parsing a RSS feed using PHP but the problem is that is has some names like:

[im:collection] => stdClass Object
  (
        [label] => Doo-Wops & Hooligans (Deluxe Version)
  )

I am doing json_decode() then:

foreach($json->content as $con){
  echo $con->im:collection->lable . "<br />";
}

but it shows an error.... plz help..

one possible way

$name_space = 'im:collection';
$con->$name_space->lable ...

/* or */
$con->{'im:collection'}->label ...

PS: personally I prefer declare a variable for reusable

If you decode the JSON as associative array (set second parameter of json_decode to true ), it becomes:

$con['im:collection']['label']

Complete:

foreach($json['content'] as $con){
    echo $con['im:collection']['label'] . "<br />";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM