Forum Moderators: coopster

Message Too Old, No Replies

Referring to a property of an object

         

george93510

9:01 pm on Nov 1, 2011 (gmt 0)

10+ Year Member



I have an object and I am having trouble referring to one of its properties. The object is $photo_data and if I do a print_r I get this (long data edited out and replaced with ...):
SimpleXMLElement Object
(
[guid] => https://picasaweb.google.com/data/entry/base/user/ ...
[pubDate] => Tue, 01 Nov 2011 05:36:45 +0000
[category] => http://schemas.google.com/photos/2007#photo
[title] => exp2Punta Chivato.jpg
[description] => ...
[enclosure] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => image/jpeg
[url] => https://lh5.googleusercontent.com/ ...
[length] => 0
)

)

[link] => https://picasaweb.google.com/ ...
)


I am trying to refer to url. My attempt was:
 $photo_data->enclosure->{'@attributes'}['url']

which is empty. What is the correct syntax?

george93510

9:59 pm on Nov 1, 2011 (gmt 0)

10+ Year Member



Apparently SimpleXML objects are not so simple and can't always be used as objects. I found the following solution from another forum, which seems to work. It uses json_encode and json_decode to convert the object into a standard array:


$photo_array = json_decode( json_encode( $photo_data ), true );
echo $photo_array['enclosure']['@attributes']['url'];

coopster

12:00 pm on Nov 3, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



When working with objects you may find var_dump() does a better job of dumping out the object definition and values.

And I also prefer json over xml any day!

george93510

10:00 pm on Nov 3, 2011 (gmt 0)

10+ Year Member



Thanks. I did also try vardump, but it gave the same structure. I discovered that the proper way to refer to this object is:

$photo_data->enclosure->attributes()->url