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

How to extract and Sort a Multidimensional Array data with PHP from a XML file (using 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. 如何将所有评论汇总在一起?...

我已经尝试了几乎所有 google 和其他论坛推荐的方法,但是无法找到如何在不编写大量代码(分隔每个数组并创建大量循环)的情况下获取这些数据......是有什么更快更简单的方法吗?

您可以使用此功能提取艺术家最流行的歌曲。它仅使用 xpath to get the Songs from AllSongs and then usort to sort that list by SongComments descending, finally using array_slice 到 return 最流行的 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。

Demo on 3v4l.org