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 am getting good actual output, eg. the line looks like

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

But when I throw it into the array, it includes all the SimpleXML wrapper tags and the jpg is not at the root of the array.

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

      

eg. the same line 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 photos at the root level, like when I echo it?

+2


a source to share


2 answers


SimpleXMLElement results look like simple objects and arrays, but they do some magic things that regular objects and arrays cannot do. It is designed in such a way that you can use less code.

You can probably solve your problem:



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 were a string, with the value being the content of the element's text. In your first example, using echo treated the element as a string, so it just returned a string.

+4


a source


I know this question is old and has already been answered, but since I had this problem today and found a relatively better and simplified solution that does not involve interleaving for loops, I want to share it with you guys.

First of all, keeping in mind your xml structure and the fact that you don't need movie information, you don't need to iterate over them, but collect all the photos, and this can only be done via xpath.

if you have multiple videos

$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 the SimpleXMLElements array, which is an array with key [0] and the value of what is the name of the image. To get the name of the image without a loop, just to get it as a string, you can simply highlight that element as a string and it will output as a string instead of SimpleXMLElement!

The only loop you might need right now:

foreach($photos as $photo) {
    dump( (string) $photo); // this will output the image name as a string e.g. "06397002.jpg"
}

      

Hi i hope this helps someone who needs this

0


a source







All Articles