簡體   English   中英

如何使用 PHP 從 XML 文件中提取和排序多維數組數據(使用 simpleXML)

[英]How to extract and Sort a Multidimensional Array data with PHP from a XML file (using simpleXML)

希望你能幫我解決我的問題...

我有這個 XML 文件,我正在使用 function 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. 如何將所有評論匯總在一起?...

我已經嘗試了谷歌和其他論壇推薦的幾乎所有東西,但是找不到如何在不編寫大量代碼(分離每個數組並創建大量循環)的情況下獲取這些數據......有沒有更快更簡單的方法來做到這一點?

您可以使用此 function 為藝術家提取最受歡迎的歌曲。 它使用xpathAllSongs中獲取Songs ,然后按usort降序對該列表進行SongComments ,最后使用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 上的演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM