开发者

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

开发者 https://www.devze.com 2023-02-02 04:24 出处:网络
I am parsing a RSS feed using PHP but the probl开发者_Go百科em is that is has some names like: [im:collection] => stdClass Object

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 />";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消