PHP: SimpleXML and Arrays

Posted by acemasta on Stack Overflow See other posts from Stack Overflow or by acemasta
Published on 2010-04-20T01:13:47Z Indexed on 2010/04/20 1:23 UTC
Read the original article Hit count: 275

Filed under:
|
|

When I run this code:

foreach($xml->movie as $movie) { if(isset($movie->photos)) { foreach ($movie->photos as $photo) { echo $photo."&nbsp;"; } echo "<hr/>"; } }

I get nice output of the actual data, e.g. a row looks like

06397001.jpg 06397002.jpg 06397003.jpg 06397004.jpg 06397005.jpg

But when I throw it in an array, it includes all the SimpleXML wrapper tags and the jpgs are not at the root of the array.

code:

foreach($xml->movie as $movie) { if(isset($movie->photos)) { $photos = array(); foreach ($movie->photos as $photo) { $photos[] = $photo; } } else $photos = ""; var_dump($photos); echo "<hr />"; }

e.g. same row looks like

array(5) { [0]=> object(SimpleXMLElement)#11 (1) { [0]=> string(12) "06397001.jpg" } [1]=> object(SimpleXMLElement)#12 (1) { [0]=> string(12) "06397002.jpg" } [2]=> object(SimpleXMLElement)#13 (1) { [0]=> string(12) "06397003.jpg" } [3]=> object(SimpleXMLElement)#14 (1) { [0]=> string(12) "06397004.jpg" } [4]=> object(SimpleXMLElement)#15 (1) { [0]=> string(12) "06397005.jpg" } }

Why is this happening/how can I remove this so I just get an array of the photos at root level like when I echo it?

Thanks, sorry for the single line code formatting; was messing up when I tried to paste directly with line breaks.

© Stack Overflow or respective owner

Related posts about php

Related posts about simplexml