如何使用PHP从XML文件中提取和排序多维数组数据(使用simpleXML)



希望你能帮助我解决我的问题...

我有这个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
)
)
)
)

问题是:

  1. 如何提取带有更多评论的前 3 首歌曲(获取评论、名称和 ID(
  2. 如何将所有评论汇总在一起?...

我已经尝试了谷歌和其他论坛推荐的几乎所有内容,但无法找到如何在不编写大量代码的情况下获取这些数据(分隔每个数组并创建大量循环(......有没有更快,更简单的方法可以做到这一点?

您可以使用此功能为艺术家提取最受欢迎的歌曲。它使用xpathAllSongs获取Songs,然后usortSongComments降序对该列表进行排序,最后使用array_slice仅返回 3 首最受欢迎的歌曲:

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

要获取每个艺术家的总评论数,您可以使用以下代码,该代码查找所有SongComment元素,将它们转换为整数并求和:

$TotalComments  = array_reduce($Artist->xpath('//SongComments'), function ($c, $v) { return $c + $v; }, 0);

对于您的示例数据,这提供了119289。

3v4l.org 演示

最新更新