<?php $xmlstr = '<?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>'; function popular_songs($Artist) { $Songs = $Artist->xpath('//Songs'); usort($Songs, function ($a, $b) { return $b->SongComments - $a->SongComments; }); return array_slice($Songs, 0, 3); } $xml = simplexml_load_string($xmlstr); foreach($xml->children() as $Artist) { $ArtistName = $Artist->ArtistName; $Streaming = $Artist->Streaming; $Fans = $Artist->Fans; $SongsArrays = $Artist->AllSongs; $SongsCount = $Artist->AllSongs->Songs->count(); $TotalComments = array_reduce($Artist->xpath('//SongComments'), function ($c, $v) { return $c + $v; }, 0); echo $TotalComments . PHP_EOL; $PopularSongs = popular_songs($Artist); print_r($PopularSongs); }
You have javascript disabled. You will not be able to edit any code.