如何在 PHP 和 PHP 中使用 foreach XML(简单XML)

发布于 2024-10-10 22:23:12 字数 2215 浏览 3 评论 0原文

有人了解 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>

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

Anyone that knows PHP and XML out there? Then please have a look!

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 mys XML 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 outcome of the code above is:

<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 it to be like this (see category on the 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: Also I wonder how to get the comma between the words, but without a comma on the last word...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浅浅 2024-10-17 22:23:12

试试这个。

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

Try this.

<?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>';
}
?>
不再让梦枯萎 2024-10-17 22:23:12

这是将 foreach 与 simplexmlElement 结合使用的方法:

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

this is how you use foreach with an simplexmlElement:

$xml = simplexml_load_file("movies.xml");
foreach ($xml->children() as $children) {
    echo $children->name;
}
一个人的夜不怕黑 2024-10-17 22:23:12
<?php
$cat_out='';
foreach($movie->regions->region->categories->categorie as $cat){
 $cat_out.= $cat.',';
}
echo rtrim($cat_out,',');
?>
<?php
$cat_out='';
foreach($movie->regions->region->categories->categorie as $cat){
 $cat_out.= $cat.',';
}
echo rtrim($cat_out,',');
?>
多情出卖 2024-10-17 22:23:12

您还需要迭代 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) ? ', ' : '';
}

You need to iterate through the categorie elements too, in the same way you've iterated through movies.

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

You'll probably want to trim the trailing , as well.


The method mentioned in my comment:

$categories = $movie->regions->region->categories->categorie;
while($category = current($categories)){
    echo $category . next($categories) ? ', ' : '';
}
花之痕靓丽 2024-10-17 22:23:12
<? foreach($movie->regions->region->categories->categorie as $category) { ?>
<p>Categori: <?= $category ?></p>
<? } ?>
<? foreach($movie->regions->region->categories->categorie as $category) { ?>
<p>Categori: <?= $category ?></p>
<? } ?>
凡间太子 2024-10-17 22:23:12
function categoryList(SimpleXmlElement $categories){
  $cats = array();
  foreach($categories as $category){
    $cats[] = (string) $category;
  }

  return implode(', ', $cats);

}

然后您可以像这样从循环中调用它:

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

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

将其隔离在函数中使其更易于维护,并且比嵌套循环更不易混淆(尽管不可否认,嵌套循环并不那么令人困惑)。

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

  return implode(', ', $cats);

}

Then you can jsut call it from your loop like:

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

Using an array and implode removes the need for logic of counting and detecting the last category.

Isolating it in a function makes it easier to maintain and is less confusing than nesting the loop (though admittedly nested loops arent that confusing).

羁绊已千年 2024-10-17 22:23:12

试试这个

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文