2011-01-09 23 views
6

PHPとXMLを知っている人は誰ですか?その後、見てください!PHPとXML(simplexml)でforeachを使用する方法

これは私の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> 

注:また、私はあなたが同じようにあなた、あまりにもcategorie要素を反復処理する必要があります...しかし、最後の単語でのコンマのない、単語の間にカンマを取得する方法を疑問に思う

答えて

1

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

+0

@TomcatExodus:+1、賢い。 – Jonah

+0

@Tomcat:+1と次の現在の使用... – prodigitalson

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

    return implode(', ', $cats); 

} 

次に、あなたのようなあなたのループからそれを呼び出すことができjsut:配列を使用して

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

これはあなたのSimpleXMLElementでのforeachを使用する方法である:

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

は、末尾のカンマを除去するために、この1

$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; 
} 
関連する問題