PHP: SimpleXML and Arrays
When I run this code:
foreach($xml->movie as $movie) {
if(isset($movie->photos)) {
foreach ($movie->photos as $photo) {
echo $photo." ";
}
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.j开发者_如何学Cpg"
}
[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?
SimpleXMLElement results look like simple objects and arrays, but do some magical things that normal objects and arrays can't do. It's designed this way so that you can use less code.
You can probably solve your problem with something like:
foreach($xml->movie as $movie) {
if(isset($movie->photos)) {
$photos = array();
foreach ($movie->photos as $photo) {
$photos[] = "$photo";
// or you could use, I believe: $photos[] = $photo[0]
}
}
else $photos = "";
var_dump($photos); echo "<hr />";
}
Why is this happening? Because a SimpleXML element when treated as a string will behave as if it's a string, with the value being that element's text contents. In your first example, the use of echo was treating the element as a string so it just returned the string.
I know this question is veeery old and it has already been answered, but since i had this issue today and i found relatively better and simplistic solution that does not involve tons of looping, I want to share it with you, guys.
First off, having in mind your xml structure, and the fact that you don't need the movie's info, you dont need to loop through them, but to gather all photos, and this can be done via xpath only.
if you have multiple movies
$photos = $xml->xpath('//movie//photos'); // double slash means get all elements with that tag name.
In other words get ALL photos from ALL movies. This code will give you an array of SimpleXMLElements which is array with key [0] and value of what the image's name is. In order to get the image name without looping just to get it as a string, you can simply cast that element as a string and it will be outputted as a string, instead of SimpleXMLElement!
The only loop you might need now is:
foreach($photos as $photo) {
dump( (string) $photo); // this will output the image name as a string e.g. "06397002.jpg"
}
Cheers, I hope it helps someone who needs it
精彩评论