【问题标题】:How to use foreach with PHP & XML (simplexml)如何在 PHP 和 XML 中使用 foreach (simplexml)
【发布时间】:2011-06-05 23:24:15
【问题描述】:

有知道 PHP 和 XML 的人吗?那么请看一下!

这是我的 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>

<? } ?>

这是我的 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>

注意:我也想知道如何获得单词之间的逗号,但最后一个单词没有逗号......

【问题讨论】:

    标签: php xml simplexml


    【解决方案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) ? ', ' : '';
    }
    

    【讨论】:

      【解决方案2】:
      <? foreach($movie->regions->region->categories->categorie as $category) { ?>
      <p>Categori: <?= $category ?></p>
      <? } ?>
      

      【讨论】:

        【解决方案3】:

        试试这个。

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

        【讨论】:

        • +1 用于去除尾随逗号。 while($categorie = current($movie-&gt;...-&gt;categorie))echo next($movie-&gt;...-&gt;categorie) ? ', ' : ''; 也可以。
        【解决方案4】:
        function categoryList(SimpleXmlElement $categories){
          $cats = array();
          foreach($categories as $category){
            $cats[] = (string) $category;
          }
        
          return implode(', ', $cats);
        
        }
        

        然后你可以直接从你的循环中调用它:

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

        使用数组和implode 消除了对计数和检测最后一个类别的逻辑的需要。

        将它隔离在一个函数中更容易维护,并且比嵌套循环更容易混淆(尽管嵌套循环确实不会令人困惑)。

        【讨论】:

          【解决方案5】:
          <?php
          $cat_out='';
          foreach($movie->regions->region->categories->categorie as $cat){
           $cat_out.= $cat.',';
          }
          echo rtrim($cat_out,',');
          ?>
          

          【讨论】:

            【解决方案6】:

            这就是你如何将 foreach 与 simplexmlElement 一起使用:

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

            【讨论】:

              【解决方案7】:

              试试这个

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

              【讨论】:

              • 这里绝对不需要json_encode+json_decode
              • 这个函数在这里返回 json Xml_to_Json 如果你想要 Xml_to_Array 然后删除 json_encode+json_decode。
              猜你喜欢
              • 2012-02-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多