2011-01-09 71 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; 
} 
相关问题