希望您能为我解决问题...
我有这个XML文件,该文件通过函数“ simplexml_load_file()”获取信息
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<Xiami>
<ArtistID>179</ArtistID>
<ArtistName>Band 1</ArtistName>
<XiamiID>9dl4gN1c745</XiamiID>
<Streaming>246</Streaming>
<Fans>84</Fans>
<TotalComments>1992</TotalComments>
<AllSongs>
<Songs>
<SongID>8ILroS998dc</SongID>
<SongName>Song 1</SongName>
<SongComments>9223</SongComments>
</Songs>
<Songs>
<SongID>8ILLD61a351</SongID>
<SongName>Song 2</SongName>
<SongComments>7221</SongComments>
</Songs>
<Songs>
<SongID>mTnf0L5d6b9</SongID>
<SongName>Song 3</SongName>
<SongComments>21212</SongComments>
</Songs>
<Songs>
<SongID>xOd2YIc7f69</SongID>
<SongName>Song 4</SongName>
<SongComments>422</SongComments>
</Songs>
<Songs>
<SongID>mTmg866314c</SongID>
<SongName>Song 5</SongName>
<SongComments>81211</SongComments>
</Songs>
</AllSongs>
</Xiami>
</rss>
这是我用来获取数据的代码:
<?php
foreach($xml->children() as $Artist) {
$ArtistName = $Artist->ArtistName;
$Streaming = $Artist->Streaming;
$Fans = $Artist->Fans;
$SongsArrays = $Artist->AllSongs;
$SongsCount = $Artist->AllSongs->Song->count();
}
?>
如果我打印“ print_r($ SongsArrays)”,则得到以下数组:
SimpleXMLElement Object
(
[Song] => Array
(
[0] => SimpleXMLElement Object
(
[SongID] => 8ILroS998dc
[SongName] => Song 1
[SongComments] => 9223
)
[1] => SimpleXMLElement Object
(
[SongID] => 8ILLD61a351
[SongName] => Song 2
[SongComments] => 7221
)
[2] => SimpleXMLElement Object
(
[SongID] => mTnf0L5d6b9
[SongName] => Song 3
[SongComments] => 21212
)
[3] => SimpleXMLElement Object
(
[SongID] => xOd2YIc7f69
[SongName] => Song 4
[SongComments] => 422
)
[4] => SimpleXMLElement Object
(
[SongID] => mTmg866314c
[SongName] => Song 5
[SongComments] => 81211
)
)
)
如果我打印整个数组,我得到这个:
SimpleXMLElement Object
(
[ArtistID] => 179
[ArtistName] => Band 1
[XiamiID] => 9dl4gN1c745
[Streaming] => 246
[Fans] => 84
[AllSongs] => SimpleXMLElement Object
(
[Song] => Array
(
[0] => SimpleXMLElement Object
(
[SongID] => 8ILroS998dc
[SongName] => Song 1
[SongComments] => 9223
)
[1] => SimpleXMLElement Object
(
[SongID] => 8ILLD61a351
[SongName] => Song 2
[SongComments] => 7221
)
[2] => SimpleXMLElement Object
(
[SongID] => mTnf0L5d6b9
[SongName] => Song 3
[SongComments] => 21212
)
[3] => SimpleXMLElement Object
(
[SongID] => xOd2YIc7f69
[SongName] => Song 4
[SongComments] => 422
)
[4] => SimpleXMLElement Object
(
[SongID] => mTmg866314c
[SongName] => Song 5
[SongComments] => 81211
)
)
)
)
问题是:
我已经尝试了Google和其他论坛推荐的几乎所有方法,但是在不编写大量代码(分离每个数组并创建许多循环)的情况下找不到如何获取此数据的方法?有没有一种更快,更简单的方法可以这样做吗?
您可以使用此功能为艺术家提取最受欢迎的歌曲。它使用xpath
从Songs
中获取AllSongs
,然后使用usort
按SongComments
降序对该列表进行排序:
function popular_songs($Artist) {
$Songs = $Artist->xpath('//Songs');
usort($Songs, function ($a, $b) { return $b->SongComments - $a->SongComments; });
return array_slice($Songs, 0, 3);
}
在循环中,您将其称为:
$PopularSongs = popular_songs($Artist);
如果您然后print_r($PopularSongs)
,您将获得(用于您的示例数据)
Array
(
[0] => SimpleXMLElement Object
(
[SongID] => mTmg866314c
[SongName] => Song 5
[SongComments] => 81211
)
[1] => SimpleXMLElement Object
(
[SongID] => mTnf0L5d6b9
[SongName] => Song 3
[SongComments] => 21212
)
[2] => SimpleXMLElement Object
(
[SongID] => 8ILroS998dc
[SongName] => Song 1
[SongComments] => 9223
)
)