[GH-ISSUE #227] Query about dealing with sort-of duplicates returned by getArtistAlbums #161

Closed
opened 2026-02-27 19:26:22 +03:00 by kerem · 2 comments
Owner

Originally created by @ghost on GitHub (Apr 4, 2021).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/227

Hi Jonathan,

I hope you're well.

This isn't so much an issue, but more of a query asking for coding advice, so I understand if you confirm you can't help since I am asking about something unrelated to your PHP wrapper.

I am using the API like this:

$options = ['return_assoc' => true];
$api = new SpotifyWebAPI\SpotifyWebAPI($options);	
$api->setAccessToken($accessToken);	
$data = $api->getArtistAlbums('5yO8MmDV2hnMbiIoZGZMGr', ['country' => 'from_token','offset' => 0,'limit' => 50,'include_groups' => 'appears_on']);

The href value returned by the API is this:

https://api.spotify.com/v1/artists/5yO8MmDV2hnMbiIoZGZMGr/albums?offset=0&limit=50&include_groups=appears_on&market=GB

The API doesn't return exact duplicates, but it does return the same album names twice, such as:

- Quarter-Life Crisis
- You & Me
- Comfortable
- Waterfall
- Postcard from Spain

For each pair of albums returned, with the same name, some of the attributes are different, such as:

- external_urls
- href
- id
- uri

However, various attributes are the same for each pair, e.g.

- name
- release_date
- total_tracks
- artists

I'd like to work out how to strip out the sort-of duplicates, but I realise they're not exactly duplicates, but if records returned have the same name, release_date, total_tracks and artists, then, maybe use that as a way to identify duplicates, and then somehow strip out the "duplicate" record, though how to work out which of the pair is the duplicate is tricky!

Also, given the above example, would it be possible to then remove the duplicates so that instead of the total record value returned by the API being 11, the actual record, minus "duplicates" would be 6.

I realise that's asking a lot, but if you have any time or advice or pointers to provide guidance please, it'd be much appreciated.

Thank you!

Jim

Originally created by @ghost on GitHub (Apr 4, 2021). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/227 Hi Jonathan, I hope you're well. This isn't so much an issue, but more of a query asking for coding advice, so I understand if you confirm you can't help since I am asking about something unrelated to your PHP wrapper. I am using the API like this: ``` $options = ['return_assoc' => true]; $api = new SpotifyWebAPI\SpotifyWebAPI($options); $api->setAccessToken($accessToken); $data = $api->getArtistAlbums('5yO8MmDV2hnMbiIoZGZMGr', ['country' => 'from_token','offset' => 0,'limit' => 50,'include_groups' => 'appears_on']); ``` The `href` value returned by the API is this: `https://api.spotify.com/v1/artists/5yO8MmDV2hnMbiIoZGZMGr/albums?offset=0&limit=50&include_groups=appears_on&market=GB` The API doesn't return exact duplicates, but it does return the same album names twice, such as: ``` - Quarter-Life Crisis - You & Me - Comfortable - Waterfall - Postcard from Spain ``` For each pair of albums returned, with the same name, some of the attributes are different, such as: ``` - external_urls - href - id - uri ``` However, various attributes are the same for each pair, e.g. ``` - name - release_date - total_tracks - artists ``` I'd like to work out how to strip out the sort-of duplicates, but I realise they're not exactly duplicates, but if records returned have the same name, release_date, total_tracks and artists, then, maybe use that as a way to identify duplicates, and then somehow strip out the "duplicate" record, though how to work out which of the pair is the duplicate is tricky! Also, given the above example, would it be possible to then remove the duplicates so that instead of the `total` record value returned by the API being 11, the actual record, minus "duplicates" would be 6. I realise that's asking a lot, but if you have any time or advice or pointers to provide guidance please, it'd be much appreciated. Thank you! Jim
kerem 2026-02-27 19:26:22 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Apr 5, 2021):

Hey Jim!
Thanks! I trust you're doing well too.

Like you're saying, it not really related to the API wrapper but it was a fun challenge to figure out so I decided to take a stab at it :)

I think this should work, it might not be the fastest solution but it should get the job done:

function unique_albums($albums) {
    // Create a new array with all albums encoded as JSON strings for PHP to compare
    $mapped_albums = array_map(function ($album) {
        // Add or remove properties as needed to get unique albums
        return json_encode([
            'artists' => $album['artists'],
            'name' => $album['name'],
            'release_date' => $album['release_date'],
            'total_tracks' => $album['total_tracks'],
        ]);
    }, $albums);

    // The actual unique filtering
    $unique_albums = array_unique($mapped_albums, SORT_STRING);

    // Restore the original values using the indexes from our original array
    $unique_albums = array_intersect_key($albums, $unique_albums);

    // Create a new array since some indexes might be missing
    return array_values($unique_albums);
}

$filtered_albums = unique_albums($data['items']);

If you have any other more general PHP/programming questions, hit me up at my email. It's in my profile.

Cheers,
Jonathan

<!-- gh-comment-id:813206908 --> @jwilsson commented on GitHub (Apr 5, 2021): Hey Jim! Thanks! I trust you're doing well too. Like you're saying, it not really related to the API wrapper but it was a fun challenge to figure out so I decided to take a stab at it :) I _think_ this should work, it might not be the fastest solution but it should get the job done: ```php function unique_albums($albums) { // Create a new array with all albums encoded as JSON strings for PHP to compare $mapped_albums = array_map(function ($album) { // Add or remove properties as needed to get unique albums return json_encode([ 'artists' => $album['artists'], 'name' => $album['name'], 'release_date' => $album['release_date'], 'total_tracks' => $album['total_tracks'], ]); }, $albums); // The actual unique filtering $unique_albums = array_unique($mapped_albums, SORT_STRING); // Restore the original values using the indexes from our original array $unique_albums = array_intersect_key($albums, $unique_albums); // Create a new array since some indexes might be missing return array_values($unique_albums); } $filtered_albums = unique_albums($data['items']); ``` If you have any other more general PHP/programming questions, hit me up at my email. It's in my profile. Cheers, Jonathan
Author
Owner

@ghost commented on GitHub (Apr 5, 2021):

Hi Jonathan. I'm doing very well, thank you :-)

Wow - that works like a charm - thank you very much indeed for taking the time to solve that challenge.

Hopefully some time in the next 20 years I'll finish this hobby project I'm working on and can send you the link once it's live.

Thanks again, and I'll email next time if I have a PHP / programming question.

Jim

<!-- gh-comment-id:813349969 --> @ghost commented on GitHub (Apr 5, 2021): Hi Jonathan. I'm doing very well, thank you :-) Wow - that works like a charm - thank you very much indeed for taking the time to solve that challenge. Hopefully some time in the next 20 years I'll finish this hobby project I'm working on and can send you the link once it's live. Thanks again, and I'll email next time if I have a PHP / programming question. Jim
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#161
No description provided.