简体   繁体   English

XML:在php中显示两个具有相同名称的子节点

[英]XML: Show two child nodes with the same name in php

I have a problem with showing my xml into a php page. 我在将xml显示到php页面时遇到问题。 I have an xml that looks like this (obviously its only a part of it because it really to long to post it all). 我有一个看起来像这样的xml(显然,它只是其中的一部分,因为真的很想发布所有内容)。 It all goes well until it cames to the "genre", I have two values for it and I don't know how to show them at the same time. 一切顺利,直到涉及到“流派”为止,我有两个值,而且我不知道如何同时显示它们。

<movie>
     <id>4441</id>
     <title>Rudderless</title>
     <title_long>Rudderless (2014)</title_long>
     <year>2014</year>
     <rating>7.5</rating>
     <runtime>105</runtime>
<genres>
   <genre>Comedy</genre>
   <genre>Drama</genre>
</genres>
</movie>

(an important thing to notice is that not every movie will have two genre, sometimes there is only one and sometime two or three) This is my code right now (需要注意的重要一点是,并非每部电影都有两种类型,有时只有一部或两部或三部)这是我现在的代码

$genere = array();
foreach ($xml->data->movies->movie->genres as $row) {
    foreach ($row->children() as $key => $val) {
        if ($key == "genre") {
            $genere[] = $val;
        }
    }
}
//and then I'll print
for ($i = 0; $i < 20 ; $i++) {
    echo "<div class=text><b>" . $genere[$i] . "</b></div>";
}

When I'm doing this it will print only for the first item of the array, and the others just give me a "Notice: Undefined offset: 1 in /path/ on line 53" 当我这样做时,它将只为数组的第一项打印,其他仅给我一个“注意:未定义的偏移量:第53行的/ path /中的1”

I've tried to follow some guides but it was a failure 我尝试遵循一些指南,但这是失败的

What am I doing wrong? 我究竟做错了什么?

/--Edit--/ / - 编辑 - /

<?php

 $xml = simplexml_load_file("https://yts.to/api/v2/list_movies.xml")
 $titolo = array();
$locandina = array();
    $anno = array();
    $durata = array();
    $genere = array();

    foreach ($xml->data->movies->movie as $element) 
    {
        foreach($element->children() as $key => $val) 
        {
            $chiave = $key;
            $valore = $val;
            if ($key == "title")
            {
                $titolo[] = $val ;
            }   
            if ($key == "medium_cover_image")
            {
                $locandina[] = $val ;
            }   
            if ($key == "year")
            {
                $anno[] = $val ;
            }   
            if ($key == "runtime")
            {
                $durata[] = $val;
            }
        }
    }
foreach ($xml->data->movies->movie->genres as $row) 
    {
        foreach($row->children() as $key => $val) 
        {
            if ($key == "genre")
            {
                $genere[] = $val;
            }
        }
    }
    var_dump($genere);

    for ($i=0 ; $i<20 ; $i++)
    {
        echo "<div class=totbox>";
        echo "<div class=box><img src=" . $locandina[$i] . "></div>";
        echo "<div class=text><b>" . $titolo[$i] . "</b> - " . $anno[$i] . "</div>";
        echo "<div class=text><b>" . $genere[$i] . "</b></div>";
        echo "<div class=text><b> Durata:" . $durata[$i] . "</b></div>";
        echo "</div>";
    }
?>

Look on my code, based on yours. 根据您的代码查看我的代码。 PHP file: PHP文件:

<?php
$xml = new SimpleXMLElement(file_get_contents('test.xml'));
$genere = array();
foreach ($xml->genres as $row) {
    foreach ($row->children() as $key => $val) {
        if ($key == "genre") {
            $genere[] = $val;
        }
    }
}
//and then I'll print
foreach ($genere as $genre) {
    echo "<div class=text><b>" . $genre . "</b></div>";
}

XML file: XML档案:

<movie>
     <id>4441</id>
     <title>Rudderless</title>
     <title_long>Rudderless (2014)</title_long>
     <year>2014</year>
     <rating>7.5</rating>
     <runtime>105</runtime>
<genres>
   <genre>Comedy</genre>
   <genre>Drama</genre>
</genres>
</movie>

Its work fine, test it. 它的工作正常,测试一下。

Based on your full xml file, look on this: 根据您的完整xml文件,查看以下内容:

<genres>
<genre>Horror</genre>
</genres>

You have only one genre on first position, so its work fine. 您在第一位置上只有一种风格,因此效果很好。 You need to loop on movie tag first, not genres. 您需要先循环播放影片标签,而不是流派。 So put your genre tag inside movie foreach. 因此,将您的流派标签放入电影foreach中。

Like: 喜欢:

    foreach($element->children() as $key => $val) 
    {
        if ($key == "genres")
        {
            // your loop, based on $val variable!
        }   

UPDATED - Code fixed. 更新-代码固定。 Please, try now. 请立即尝试。

<?php
$xml = simplexml_load_file("https://yts.to/api/v2/list_movies.xml")
$titolo = array();
$locandina = array();
$anno = array();
$durata = array();
$genere = array();

    $i = 0;
    foreach ($xml->data->movies->movie as $element) {
        foreach ($element->children() as $key => $val) {
            $chiave = $key;
            $valore = $val;
            if ($key == "title") {
                $titolo[] = $val;
            }
            if ($key == "medium_cover_image") {
                $locandina[] = $val;
            }
            if ($key == "year") {
                $anno[] = $val;
            }
            if ($key == "runtime") {
                $durata[] = $val;
            }
            if ($key == "genres") {

                for($g = 0; $g < count($xml->data->movies->movie[$i]->genres->genre); $g++) {
                        $genere[$i][] = $xml->data->movies->movie[$i]->genres->genre[$g];
                }
            }
        }
        $i++;
    }



    for ($i = 0; $i < count($titolo); $i++) {
        echo "<div class=totbox>";
        if (isset($locandina[$i])) {
            echo "<div class=box><img src=" . $locandina[$i] . "></div>";
        }
        echo "<div class=text><b>" . $titolo[$i] . "</b> - " . $anno[$i] . "</div>";
        foreach ($genere[$i] as $genResult) {
            echo "<div class=text><b>" . $genResult . "</b></div>";
        }

        echo "<div class=text><b> Durata:" . $durata[$i] . "</b></div>";
        echo "</div>";
    }

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM