How to use foreach with PHP and XML (simplexml)

Does anyone know PHP and XML there? Then please watch!

This is my PHP code:

<? $xml = simplexml_load_file("movies.xml"); foreach ($xml->movie as $movie){ ?> <h2><? echo $movie->title ?></h2> <p>Year: <? echo $movie->year ?></p> <p>Categori: <? echo $movie->regions->region->categories->categorie ?></p> <p>Country: <? echo $movie->countries->country ?></p> <? } ?> 

This is the mysql file:

 <?xml version="1.0" encoding="UTF-8"?> <movies> <movie> <title>A movie name</title> <year>2010</year> <regions> <region> <categories> <categorie id="3">Animation</categorie> <categorie id="5">Comedy</categorie> <categorie id="9">Family</categorie> </categories> <countries> <country id="123">USA</country> </countries> </region> </regions> </movie> <movie> <title>Little Fockers</title> <year>2010</year> <regions> <region> <categories> <categorie id="5">Comedy</categorie> </categories> <countries> <country id="123">USA</country> </countries> </region> </regions> </movie> </movies> 

The result of the above code:

 <h2>A movie name</h2> <p>Year: 2010</p> <p>Category: Animation</p> <p>Country: USA</p> <h2>Little Fockers</h2> <p>Year: 2010</p> <p>Category: Comedy</p> <p>Country: USA</p> 

I want this to be so (see category in first movie):

 <h2>A movie name</h2> <p>Year: 2010</p> <p>Category: Animation, Comedy, Family</p> <p>Country: USA</p> <h2>Little Fockers</h2> <p>Year: 2010</p> <p>Category: Comedy</p> <p>Country: USA</p> 

Note. It is also interesting how to get a comma between words, but without a comma on the last word ...

+6
xml php simplexml
source share
7 answers

Try it.

 <?php $xml = simplexml_load_file("movies.xml"); foreach ($xml->movie as $movie) { echo '<h2>' . $movie->title . '</h2>'; echo '<p>' . $movie->year . '</p>'; $categories = $movie->regions->region->categories->categorie; while ($categorie = current($categories)) { echo $categorie; echo next($categories) ? ', ' : null; } echo '<p>' . $movie->countries->country . '</p>'; } ?> 
+19
source share

this is how you use foreach with simplexmlElement:

 $xml = simplexml_load_file("movies.xml"); foreach ($xml->children() as $children) { echo $children->name; } 
+4
source share

You need to iterate through the categorie elements, just as you did iteration with movies .

 echo '<p>'; foreach($movie->regions->region->categories->categorie as $categorie){ echo $categorie . ', '; } echo '</p>'; 

You probably also want to trim the final one,.


The method mentioned in my comment:

 $categories = $movie->regions->region->categories->categorie; while($category = current($categories)){ echo $category . next($categories) ? ', ' : ''; } 
+1
source share
 <?php $cat_out=''; foreach($movie->regions->region->categories->categorie as $cat){ $cat_out.= $cat.','; } echo rtrim($cat_out,','); ?> 
+1
source share
 <? foreach($movie->regions->region->categories->categorie as $category) { ?> <p>Categori: <?= $category ?></p> <? } ?> 
0
source share
 function categoryList(SimpleXmlElement $categories){ $cats = array(); foreach($categories as $category){ $cats[] = (string) $category; } return implode(', ', $cats); } 

Then you can call it from your loop like this:

 <? echo categoryList($movie->regions->region->categories); ?> 

Using an array and implode eliminates the need for logic to count and detect the last category.

Isolation in a function simplifies its support and is less confusing than nesting a loop (although admittedly nested loops arent confusing).

0
source share

Try this one

 $xml = ... // Xml file data $Json = Xml_to_Json($xml); $array = json_decode($Json,true); echo '<pre>'; print_r($array); foreach ($array as $key => $value) { foreach ($value as $key1 => $value1) { echo '<h2>'.$value1['title'].'</h2> <p>Year: '.$value1['year'].'</p> <p>Category: '; foreach ($value1['regions']['region']['categories']['categorie'] as $categoriekey => $categorie) { echo $categorie.' '; } echo 'Animation</p> <p>Country: '.$value1['regions']['region']['countries']['country'].'</p>'; } } function Xml_to_Json($array){ $xml = simplexml_load_string($array, "SimpleXMLElement", LIBXML_NOCDATA); $json = json_encode($xml); return $json; } 
0
source share

All Articles