How to access a JSON element which has ':' in its name using PHP
I am parsing a RSS feed using PHP but the probl开发者_Go百科em 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 />";
}
精彩评论