PHP load multiple XML data without numbering

I'm facing some problems trying to load multiple data from an XML file. I want to load all XML data without having to number them [0],[1], etc. how can I accomplish that?
PHP
<?php
echo"<center>";
$xml = simplexml_load_file('channels.xml') or die('Failed to read data');
echo $xml->channels[0]->name . "<br>";
echo "<img class='responsive' src='";
echo $xml->channels[0]->banner;
echo "'><br>";
echo "<form method='post' action='index.php'>";
echo "<input type='hidden' type='text' name='url' value='";
echo $xml->channels[0]->url;
echo "'><br>";
echo "<input type='submit' name='submit' value='PLAY CHANNEL'><br>";
$xml = simplexml_load_file('channels.xml') or die('Failed to read data');
echo $xml->channels[1]->name . "<br>";
echo "<img class='responsive' src='";
echo $xml->channels[1]->banner;
echo "'><br>";
echo "<form method='post' action='index.php'>";
echo "<input type='hidden' type='text' name='url' value='";
echo $xml->channels[1]->url;
echo "'><br>";
echo "<input type='submit' name='submit' value='PLAY CHANNEL'><br>";
echo"</center>";
if(isset($_POST['url'])) {
shell_exec("sudo killall player.bin");
shell_exec("sudo player `youtube-dl -g ".($_POST['url'])." `>> /dev/null &");
}
?>
XML
<?xml version="1.0" encoding="utf-8"?>
<data>
<channels>
<banner>https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/Sky-news-logo.svg/768px-Sky-news-logo.svg.png</banner>
<url>https://www.youtube.com/channel/UCoMdktPbSTixAyNGwb-UYkQ/live</url>
</channels>
<channels>
<banner>https://lh3.googleusercontent.com/proxy/DqTJr6u-LpXfWyrJscDsoa_XdZsXLTm1PXDLw_eJYRAlwx0RtNO4thah_YQhKk-vWmeSHkIfSGsClFIQmTWDS0iNGM3VeeEXrSzYSVPRFyEf5NQ9TVw0B9KYXg</banner>
<url>https://www.youtube.com/channel/UCeY0bbntWzzVIaj2z3QigXg/live</url>
</channels>
</data>
Answer
Solution:
As shown in the examples in the PHP manual, you can loop over elements with the same name with a normalforeach
loop:
$xml = simplexml_load_file('channels.xml') or die('Failed to read data');
foreach ( $xml->channels as $channelsElement ) {
echo $channelsElement->name;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: installation failed, reverting ./composer.json and ./composer.lock to their original content
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.