簡體   English   中英

數組分組-如何構建API響應

[英]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.

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