![](/img/trans.png)
[英]how to parse xml file using simplexml and create multidimensional array in PHP
[英]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
)
)
)
)
問題是:
我已經嘗試了谷歌和其他論壇推薦的幾乎所有東西,但是找不到如何在不編寫大量代碼(分離每個數組並創建大量循環)的情況下獲取這些數據......有沒有更快更簡單的方法來做到這一點?
您可以使用此 function 為藝術家提取最受歡迎的歌曲。 它使用xpath
從AllSongs
中獲取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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.