Ɛɔıs3
Ɛɔıs3

Reputation: 7853

Prevent having several times the same array in php

I use the mikealmond tool to parse the PHP API of MusicBrainz (the documentation is available here). I face such a simple problem that I can't understand what I'm doing wrong. I try to recover all recordings for a given artist (by David Guetta for my tests).

Here is the code :

public function getRecordingsWithArtistMbdi() {
    $arrayRecordings = array();
    $includes = array('artist-credits', 'tags');
    try {
        $recordings = $this->mb->browseRecording('artist', '302bd7b9-d012-4360-897a-93b00c855680', $includes);
        $countRecordings = $recordings['recording-count'];
        foreach ($recordings['recordings'] as $recording) {
            if (!in_array($recording['id'], $arrayRecordings)) {
                $record = array(
                    'titre' => isset($recording['title']) ? $recording['title'] : null,
                    'duree' => isset($recording['length']) ? $recording['length'] : null,
                    'tags' => isset($recording['tags']) ? $recording['tags'] : null,
                    'artist-credit' => isset($recording['artist-credit']) ? $recording['artist-credit'] : null
                );
                $arrayRecordings[$recording['id']] = $record;
            }
            var_dump($arrayRecordings);
        }
    } catch (Exception $e) {
        print $e->getMessage();
    }
}

And here is the beginning of what I get as a result of var_dump :

array (size=1)
  '11329ba1-2645-4bbb-92ae-b2929188d51c' => // Ok, it is saved now
    array (size=4)
      'titre' => string 'ACDC' (length=4)
      'duree' => int 241786
      'tags' => 
        array (size=1)
          0 => 
            array (size=2)
              ...
      'artist-credit' => 
        array (size=1)
          0 => 
            array (size=3)
              ...
array (size=2)
  '11329ba1-2645-4bbb-92ae-b2929188d51c' => // Double information
    array (size=4)
      'titre' => string 'ACDC' (length=4)
      'duree' => int 241786
      'tags' => 
        array (size=1)
          0 => 
            array (size=2)
              ...
      'artist-credit' => 
        array (size=1)
          0 => 
            array (size=3)
              ...
  '124c53b9-6cc5-4f46-a811-598e8f2947fb' => 
    array (size=4)
      'titre' => string 'Baby When the Light' (length=19)
      'duree' => null
      'tags' => 
        array (size=0)
          empty
      'artist-credit' => 
        array (size=1)
          0 => 
            array (size=3)
              ...

As you can see, the first array is recorded but despite the if (!in_array($recording['id'], $arrayRecordings)) {, it is learned each time. How to solve this problem with my current code?

It is pure php problem, the problem is not with the parse of API !

Upvotes: 2

Views: 69

Answers (1)

Arnold Daniels
Arnold Daniels

Reputation: 16573

in_array looks for a value inside the array. In you case the values are associated arrays. You may want to check if the key exists

if (!isset($arrayRecordings[$recording['id']]))

Regardless, the output won't change much, since your loop is replacing the duplicate data. Writing to the same key in an error overwrites the existing data. So there are no duplicates in the $arrayRecordings array.

Do note that you're doing a var_dump inside of the look, causing you to see each state of the array as it's being build. You might be expecting to so the end result only.

If you do the var_dump outside of the foreach loop, you'll see that $arrayRecordings doesn't contain any duplicates.

Upvotes: 2

Related Questions