简体   繁体   中英

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

I have a problem with showing my xml into a php page. I have an xml that looks like this (obviously its only a part of it because it really to long to post it all). 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"

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
$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:

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

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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