简体   繁体   中英

XML - Nodes with the same name showing only one time in PHP

I have a little problem with decoding my xml with php:

I have an xml file with details about movies, what I need to do is to take the nodes torrents->torrent->quality and show the result in my php script. Right now I have a part of the code, it shows me only the first quality that it finds.

(For example: I have the movie "Poltergeist" that is in 720p and 1080p, but in my script it will only show the 720p and skip the 1080p)

Screenshot website: 在此处输入图片说明

Screenshot xml: 在此处输入图片说明

<link rel="stylesheet" type="text/css" href="css/style.css"> 
<?php



$xml = simplexml_load_file("https://yts.to/api/v2/list_movies.xml?limit=10");
$titolo = array();
$locandina = array();
$anno = array();
$durata = array();
$genere = array();
$qualita = 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];
                }
            }   

            $qualdef = $xml->data->movies->movie->torrents->torrent;

         foreach ($qualdef as $element) {
            foreach ($element->children() as $key => $val) {
             if ($key == "quality") {
                 $qualita[] = $val;
             }
            }   
         }  



        }
        $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> Qualita':" . $qualita[$i] . "</b></div>";
        echo "<div class=text><b> Durata:" . $durata[$i] . "</b></div>";
        echo "</div>";
    }


?>

What am I doing wrong?

It seems like you are collecting data without any problem but when you are displaying you go by the index of $titolo so you don't go through every element of $qualita array.

So just try this:

echo "<div class=text><b> Qualita':" . implode(', ',$qualita) . "</b></div>";

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