我有一个 XML 文件并将其解析到我的 PHP 文档中。我想按字母顺序对 XML 的子节点进行排序,并将它们显示在我的选择框中。有人可以帮帮我吗?我被困在排序过程中...... :)
国家.xml
<?xml version="1.0"?>
<countries>
<country>
<name>Deutschland</name>
<league>Bundesliga</league>
</country>
<country>
<name>Frankreich</name>
<league>Ligue 1</league>
</country>
<country>
<name>Osterreich</name>
<league>Tipp3-Bundesliga</league>
</country>
<country>
<name>England</name>
<league>Premier League</league>
</country>
<country>
<name>Schweden</name>
<league>Allsvenskan</league>
</country>
<country>
<name>Kanada</name>
<league>Canadian Soccer League</league>
</country>
</countries>
我的 PHP 代码如下所示:
echo "<select>";
foreach ($newXml as $item) {
$country=$item->name;
$league=$item->league;
echo $league;
echo "<option>".$country."-".$league."</option>";
}
echo "</select>";