2011-01-09 37 views
6

任何知道PHP和XML的人都有嗎?那麼請看看!如何在PHP和XML中使用foreach(simplexml)

這是我的PHP代碼:

<? $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> 

<? } ?> 

這是MYS XML文件:

<?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> 

代碼的結果上面是:

<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> 

我想它就像這樣(參見第一部電影類別):

<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> 

注:另外我想知道如何得到詞之間的逗號,但沒有對最後一個字一個逗號......

回答

1

您需要通過categorie元素迭代也以同樣的方式你已經通過movies迭代。

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

您可能還想修剪尾部,


在我的評論中提到的方法:

$categories = $movie->regions->region->categories->categorie; 
while($category = current($categories)){ 
    echo $category . next($categories) ? ', ' : ''; 
} 
0
<? foreach($movie->regions->region->categories->categorie as $category) { ?> 
<p>Categori: <?= $category ?></p> 
<? } ?> 
19

試試這個。

<?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>'; 
} 
?> 
+7

+1剝出後面的逗號。 `while($ categorie = current($ movie - > ...-> categorie))`和`echo next($ movie - > ...-> categorie)? ',':'';`也可以。 – Dan 2011-01-09 04:18:04

0
function categoryList(SimpleXmlElement $categories){ 
    $cats = array(); 
    foreach($categories as $category){ 
    $cats[] = (string) $category; 
    } 

    return implode(', ', $cats); 

} 

然後你可以僅僅指剛從你的循環喜歡叫它:

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

使用數組和implode消除了計數的邏輯和檢測最後一個類別的需要。

在一個函數中進行隔離使其更容易維護,並且比嵌套循環更容易混淆(儘管被公認的嵌套循環並不是混淆)。

1
<?php 
$cat_out=''; 
foreach($movie->regions->region->categories->categorie as $cat){ 
$cat_out.= $cat.','; 
} 
echo rtrim($cat_out,','); 
?> 
4

你這是怎麼使用的foreach用的SimpleXMLElement:

$xml = simplexml_load_file("movies.xml"); 
foreach ($xml->children() as $children) { 
    echo $children->name; 
} 
0

試試這個

$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; 
}