I wonder why the SimpleXMLELement object seems weird, that when we var_dump($entry): object(SimpleXMLElement)#4 (8) { ["id"]=> string(53) "http://gdata.youtube.com/feeds/api/videos/YkA31t0n1Ew" ["published"]=> string(24) "2005-12-24T05:14:40.000Z" ... ["title"]=> string(46) "two chinese boys:bu de bu ai(ä¸Âå¾—ä¸Â愛)" } Code (markup): so when we var_dump($entry->id), we'd expect it is a string, but no, we actually get another SimpleXMLElement object instead of a string object(SimpleXMLElement)#5 (1) { [0]=> string(53) "http://gdata.youtube.com/feeds/api/videos/YkA31t0n1Ew" } Code (markup): and the same, if we var_dump($entry->title), we also get another SimpleXMLElement object object(SimpleXMLElement)#5 (2) { ["@attributes"]=> array(1) { ["type"]=> string(4) "text" } [0]=> string(46) "two chinese boys:bu de bu ai(ä¸Âå¾—ä¸Â愛)" } Code (markup): so i just wonder, how come it behaves like this, and how do we get the "id" and "title" properly? thanks very much.
I forgot exactly why it does that (or I didn't know in the first place) but you can use typecasting: echo (string) $entry->title; PHP:
I think you're misunderstanding how objects work...if you var_dump the object then it's gonna return the object+anything within you specify; i'm assuming $entry was already an instatiated SimpleXML object, so instead try print_r($entry) or echo $entry['title'] etc...let me know how it goes
same thing. i used print_r(): print_r($ns_media); print_r("foo " . $ns_media['title'] . " bar"); print_r("foo2 " . $ns_media->title . " bar2"); and they give SimpleXMLElement Object ( ... [title] => Saving Our Economy: What'$ Next? [description] => It's the economic crisis of the century; our entire nation is at risk. Who's to blame and how do we fix it? FOX Business anchor David Asman hosts an in-depth investigation into how we got here. ... ) foo bar foo2 Saving Our Economy: What'$ Next? bar2 the second print_r() gave nothing between the "foo bar". if using var_dump() it gives NULL
ok, if you need a sample, try $simpleXMLObj = simplexml_load_string( '<?xml version="1.0" encoding="UTF-8"?> <publisher:Item xmlns:publisher="http://www.whatever.com/"> <entry> <name>How to clean everything</name> <price>$15.02</price> <publisher:name>Random House</publisher:name> <publisher:address>USA</publisher:address> </entry> <entry> <name><![CDATA[Paris Tour book]]></name> <price>$11.99</price> <publisher:name><![CDATA[Lonely Planet]]></publisher:name> <publisher:address>France</publisher:address> </entry> </publisher:Item>'); print_r($simpleXMLObj->entry); print_r($simpleXMLObj->entry->name); Code (markup): the output is: SimpleXMLElement Object ( [name] => How to clean everything [price] => $15.02 ) SimpleXMLElement Object ( [0] => How to clean everything ) Code (markup):
now that we've established print_r seems to work, what happens when you try echo $simpleXMLObj->entry; should output 'Array' and echo $simpleXMLObj->entry['name']; should output 'How to clean everything' ???
I don't get it - of course you get an array output if you use something "array'y" to display it - for instance print_r Why not just use "echo ($simpleXMLObj->entry->name);" and so forth?
NO, my real question is, why print_r($simpleXMLObj->entry); prints out an object with a instance variable "name" as a string, and then when i print out that instance variable, it is a SimpleXMLElement object.
with other objects, if you print_r($obj) and you see an instance variable "name" that is a string, then you print_r($obj->name) and it will be a string. in the case above, it is NOT. it is an object. WHY?
looking at the documentation, http://ie.php.net/manual/en/function.simplexml-load-string.php. obviously, it's because entry has some members and they become arrays. and the reason it's an object is because that's what simplexml_load_string does.. and if that's your question, have you already solved how to echo out strings? coz i'd like to know how you do it, as there's more than one 'entry' group in the object.
Okay, lets try this very slowly - you're not printing objects - you are printing arrays with the keys and values attached, ok? print_r($simpleXMLObj->entry); prints the array of the Entry-part, which, naturally, contains the [name] and [price] strings. When you then print_r($simpleXMLObj->entry->name); it PRINTS OUT THE ARRAY OF THE "name"-part - which will be SimpleXMLElement Object ( [0] => How to clean everything ) <- an array. Ok? If you simply PRINT or ECHO the $simpleXMLObj->entry->name value, you will get the string stored in "name" as a value, and it will show. What is confusing?
for example, $obj = new stdClass(); $obj->name = "hello world"; $obj->description = "this is a string of something"; print_r($obj); print_r($obj->name); Code (markup): the result is stdClass Object ( [name] => hello world [description] => this is a string of something ) hello world Code (markup): you can see that the second print_r didn't give an object, unlike the SimpleXMLElement case we see above.
i don't see any "nested array" but i also don't see any nested array here too: SimpleXMLElement Object ( [name] => How to clean everything [price] => $15.02 ) SimpleXMLElement Object ( [0] => How to clean everything ) Code (markup):