[英]Array Grouping - How to structure a api response
好的,问题是出价奇怪,因为我不确定如何用简短的标题很好地解释它。
所以这是我的想法(只是让它更有意义)
我搞砸了spotify api,并试图保存我的所有歌曲(将它们导出),因为我有很多歌曲,如果Spotify弄乱了某些东西而一切变坏了,我仍然会列出所有我的歌曲。
所以这是我的问题。
我设法获得了所有歌曲和Artits的名字。 Prbolem是我有“更多”的艺术家然后有歌曲,因为有1个或更多的人在唱歌(功能)
这是一些代码,可能可以解决问题...
$tracksData = $this->getAllPlaylistTracksData('PLAYLISTID');
var_dump($tracksData);
$allTracksName = [];
foreach ($tracksData as $trackData) {
$artistsData = $trackData->track->artists;
var_dump($artistsData);
$allTracksName[] = $trackData->track->name;
}
return $allTracksName;
这是两个转储输出:
所有乐曲数据:
array (size=2135)
0 =>
object(stdClass)[4]
public 'added_at' => string '2014-01-06T12:26:52Z' (length=20)
public 'added_by' =>
object(stdClass)[8]
public 'external_urls' =>
object(stdClass)[5]
...
public 'href' => string '' (length=43)
public 'id' => string '' (length=10)
public 'type' => string 'user' (length=4)
public 'uri' => string 'spotify:user:' (length=23)
public 'is_local' => boolean false
public 'primary_color' => null
public 'track' =>
object(stdClass)[16]
public 'album' =>
object(stdClass)[9]
...
public 'artists' =>
array (size=1)
...
public 'available_markets' =>
array (size=0)
...
public 'disc_number' => int 1
public 'duration_ms' => int 255093
public 'episode' => boolean false
public 'explicit' => boolean false
public 'external_ids' =>
object(stdClass)[19]
...
public 'external_urls' =>
object(stdClass)[20]
...
public 'href' => string 'https://api.spotify.com/v1/tracks/3zKST4nk4QJE77oLjUZ0Ng' (length=56)
public 'id' => string '3zKST4nk4QJE77oLjUZ0Ng' (length=22)
public 'is_local' => boolean false
public 'name' => string 'Hey Brother' (length=11)
public 'popularity' => int 22
public 'preview_url' => null
public 'track' => boolean true
public 'track_number' => int 3
public 'type' => string 'track' (length=5)
public 'uri' => string 'spotify:track:3zKST4nk4QJE77oLjUZ0Ng' (length=36)
public 'video_thumbnail' =>
object(stdClass)[21]
public 'url' => null
1 =>
所有Artits数据:
array (size=1)
0 =>
object(stdClass)[18]
public 'external_urls' =>
object(stdClass)[17]
public 'spotify' => string 'https://open.spotify.com/artist/1vCWHaC5f2uS3yhpwWbIA6' (length=54)
public 'href' => string 'https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6' (length=57)
public 'id' => string '1vCWHaC5f2uS3yhpwWbIA6' (length=22)
public 'name' => string 'Avicii' (length=6)
public 'type' => string 'artist' (length=6)
public 'uri' => string 'spotify:artist:1vCWHaC5f2uS3yhpwWbIA6' (length=37)
**array (size=1)**
0 =>
object(stdClass)[34]
public 'external_urls' =>
object(stdClass)[33]
public 'spotify' => string 'https://open.spotify.com/artist/7Ln80lUS6He07XvHI8qqHH' (length=54)
public 'href' => string 'https://api.spotify.com/v1/artists/7Ln80lUS6He07XvHI8qqHH' (length=57)
public 'id' => string '7Ln80lUS6He07XvHI8qqHH' (length=22)
public 'name' => string 'Arctic Monkeys' (length=14)
public 'type' => string 'artist' (length=6)
public 'uri' => string 'spotify:artist:7Ln80lUS6He07XvHI8qqHH' (length=37)
array (size=4)
0 =>
object(stdClass)[52]
public 'external_urls' =>
object(stdClass)[51]
public 'spotify' => string 'https://open.spotify.com/artist/6yTYR09WCvsgdnurDW4WQo' (length=54)
public 'href' => string 'https://api.spotify.com/v1/artists/6yTYR09WCvsgdnurDW4WQo' (length=57)
public 'id' => string '6yTYR09WCvsgdnurDW4WQo' (length=22)
public 'name' => string 'DJ Antoine' (length=10)
public 'type' => string 'artist' (length=6)
public 'uri' => string 'spotify:artist:6yTYR09WCvsgdnurDW4WQo' (length=37)
1 =>
object(stdClass)[54]
public 'external_urls' =>
object(stdClass)[53]
public 'spotify' => string 'https://open.spotify.com/artist/3yBEHAINorE3uSvsCmyCwQ' (length=54)
public 'href' => string 'https://api.spotify.com/v1/artists/3yBEHAINorE3uSvsCmyCwQ' (length=57)
public 'id' => string '3yBEHAINorE3uSvsCmyCwQ' (length=22)
public 'name' => string 'Mad Mark' (length=8)
public 'type' => string 'artist' (length=6)
public 'uri' => string 'spotify:artist:3yBEHAINorE3uSvsCmyCwQ' (length=37)
2 =>
object(stdClass)[56]
public 'external_urls' =>
object(stdClass)[55]
public 'spotify' => string 'https://open.spotify.com/artist/1o9x7ez0NvhDb7gBqrYnUj' (length=54)
public 'href' => string 'https://api.spotify.com/v1/artists/1o9x7ez0NvhDb7gBqrYnUj' (length=57)
public 'id' => string '1o9x7ez0NvhDb7gBqrYnUj' (length=22)
public 'name' => string 'B-Case' (length=6)
public 'type' => string 'artist' (length=6)
public 'uri' => string 'spotify:artist:1o9x7ez0NvhDb7gBqrYnUj' (length=37)
3 =>
object(stdClass)[58]
public 'external_urls' =>
object(stdClass)[57]
public 'spotify' => string 'https://open.spotify.com/artist/5FTpdDUA9cksspPW5Ix78g' (length=54)
public 'href' => string 'https://api.spotify.com/v1/artists/5FTpdDUA9cksspPW5Ix78g' (length=57)
public 'id' => string '5FTpdDUA9cksspPW5Ix78g' (length=22)
public 'name' => string 'U-Jean' (length=6)
public 'type' => string 'artist' (length=6)
public 'uri' => string 'spotify:artist:5FTpdDUA9cksspPW5Ix78g' (length=37)
K:\programming\development\spotify-backup\Utility\Manager.php:49:
array (size=2)
0 =>
object(stdClass)[74]
public 'external_urls' =>
object(stdClass)[73]
public 'spotify' => string 'https://open.spotify.com/artist/1h6Cn3P4NGzXbaXidqURXs' (length=54)
public 'href' => string 'https://api.spotify.com/v1/artists/1h6Cn3P4NGzXbaXidqURXs' (length=57)
public 'id' => string '1h6Cn3P4NGzXbaXidqURXs' (length=22)
public 'name' => string 'Swedish House Mafia' (length=19)
public 'type' => string 'artist' (length=6)
public 'uri' => string 'spotify:artist:1h6Cn3P4NGzXbaXidqURXs' (length=37)
1 =>
object(stdClass)[76]
public 'external_urls' =>
object(stdClass)[75]
public 'spotify' => string 'https://open.spotify.com/artist/2auikkNYqigWStoHWK1Grq' (length=54)
public 'href' => string 'https://api.spotify.com/v1/artists/2auikkNYqigWStoHWK1Grq' (length=57)
public 'id' => string '2auikkNYqigWStoHWK1Grq' (length=22)
public 'name' => string 'John Martin' (length=11)
public 'type' => string 'artist' (length=6)
public 'uri' => string 'spotify:artist:2auikkNYqigWStoHWK1Grq' (length=37)
如您所知,第二个转储是我的播放列表1:1
现在我的问题是我不确定如何对它们进行分组,是的,我尝试像这样遍历它们
foreach ($artistsData as $artistData) {
$allArtistsName[] = $artistData->name;
}
但这仅给我留下了两个庞大的数组,它们的大小各不相同,因为除了歌曲外还有“更多”的艺术作品。
有谁知道我该如何解决这个问题?
编辑:
我想要这样的东西
0 => [
songName => i
Artists = > x y z
]
您可以在var_dump
中看到在他的歌曲数组中找到了每个歌手-因此您可以在songs数组中添加另一个循环并提取每首歌曲的歌手列表:
$tracksData = $this->getAllPlaylistTracksData('PLAYLISTID');
$songs = [];
foreach ($tracksData as $trackData) {
$artistsNamesForSong = []; // init new artists array for each song
foreach($trackData->track->artists as $artist) {
$artistsNamesForSong[] = $artist->name;
}
$songs[] = ["songName" => , "Artists" => $artistsNamesForSong];
}
return $songs;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.