2015-04-14 5 views
-1

У меня такой json-файл. Он содержит популярные песни по годам. Я буду использовать эти песни для создания плейлистов, основанных на десятилетиях позже.Как удалить повторяющиеся элементы в многомерном массиве?

{ 
    "1940": [ 
     { 
      "artist": "Tommy Dorsey", 
      "title": "I'll Never Smile Again" 
     }, 
     { 
      "artist": "Bing Crosby", 
      "title": "Only Forever" 
     }, 
     { 
      "artist": "Artie Shaw", 
      "title": "Frenesi" 
     }, 
     { 
      "artist": "Glenn Miller", 
      "title": "The Nearness of You" 
     }, 
     { 
      "artist": "Artie Shaw", 
      "title": "Frenesi" 
     }, 
     { 
      "artist": "Tommy Dorsey & Frank Sinatra", 
      "title": "Imagination" 
     }, 
     { 
      "artist": "Cliff Edwards (Ukelele Ike)", 
      "title": "When You Wish Upon a Star" 
     }, 
     { 
      "artist": "Bing Crosby", 
      "title": "Sierra Sue" 
     }, 
     { 
      "artist": "The Ink Spots", 
      "title": "Java Jive" 
     }, 
     { 
      "artist": "Jimmy Dorsey", 
      "title": "6 Lessons From Madame La Zonga" 
     }, 
     { 
      "artist": "Jimmie Davis", 
      "title": "You Are My Sunshine" 
     }, 
     { 
      "artist": "Bob Wills & his Texas Playboys", 
      "title": "New San Antonio Rose" 
     }, 
     { 
      "artist": "Tommy Dorsey", 
      "title": "Stardust" 
     }, 
     { 
      "artist": "The Andrews Sisters", 
      "title": "Beat Me Daddy, Eight To The Bar" 
     }, 
     { 
      "artist": "Charlie Barnet", 
      "title": "Pompton Turnpike" 
     }, 
     { 
      "artist": "Judy Garland", 
      "title": "I'm Nobody's Baby" 
     }, 
     { 
      "artist": "Bob Crosby & his Orchestra", 
      "title": "Down Argentina Way" 
     }, 
     { 
      "artist": "Kate Smith", 
      "title": "The Woodpecker Song" 
     }, 
     { 
      "artist": "Shep Fields & his Rippling Rhythm Orchestra", 
      "title": "Down Argentina Way" 
     }, 
     { 
      "artist": "Vaughn Monroe", 
      "title": "There I Go" 
     }, 
     { 
      "artist": "Will Bradley", 
      "title": "There I Go" 
     }, 
     { 
      "artist": "Mitchell Ayres & his Fashions in Music", 
      "title": "Make Believe Island" 
     }, 
     { 
      "artist": "Joe Loss Orchestra", 
      "title": "Beer Barrel Polka (Roll out the Barrel)" 
     }, 
     { 
      "artist": "Carson Robison", 
      "title": "South of the Border (Down Mexico Way)" 
     }, 
     { 
      "artist": "Francisco Alves", 
      "title": "Dama das Camelias" 
     }, 
     { 
      "artist": "Duke Ellington", 
      "title": "Ko Ko" 
     }, 
     { 
      "artist": "Bill Monroe", 
      "title": "Mule Skinner Blues" 
     }, 
     { 
      "artist": "Bukka White", 
      "title": "Special Stream Line" 
     }, 
     { 
      "artist": "Tony Martin", 
      "title": "It's a Blue World" 
     }, 
     { 
      "artist": "Orlando Silva", 
      "title": "Suplica" 
     }, 
     { 
      "artist": "Ella Fitzgerald", 
      "title": "Five O'Clock Whistle" 
     }, 
     { 
      "artist": "Edith Piaf", 
      "title": "L'accordeoniste" 
     }, 
     { 
      "artist": "Erskine Hawkins", 
      "title": "Dolemite" 
     }, 
     { 
      "artist": "Dick Robertson & his Orchestra", 
      "title": "Ma (He's Making Eyes At Me)" 
     }, 
     { 
      "artist": "Kay Kyser", 
      "title": "Playmates" 
     }, 
     { 
      "artist": "Nat Gonella", 
      "title": "I'm Nobody's Baby" 
     }, 
     { 
      "artist": "Bando da Lua", 
      "title": "O Samba da Minha Terra" 
     }, 
     { 
      "artist": "Cyro Monteiro", 
      "title": "Oh! Seu Oscar" 
     }, 
     { 
      "artist": "Roland Hayes", 
      "title": "Were You There?" 
     }, 
     { 
      "artist": "Benny Goodman", 
      "title": "Seven Come Eleven" 
     }, 
     { 
      "artist": "Sammy Kaye", 
      "title": "Dream Valley" 
     }, 
     { 
      "artist": "Bob Chester & his Orchestra", 
      "title": "Practice Makes Perfect" 
     }, 
     { 
      "artist": "Frank Proffitt", 
      "title": "Tom Dooley" 
     }, 
     { 
      "artist": "Art Tatum", 
      "title": "Sweet Lorraine" 
     }, 
     { 
      "artist": "Dircinha Batista", 
      "title": "Upa! Upa! (Meu Trolinho)" 
     }, 
     { 
      "artist": "Connee Boswell", 
      "title": "On the Isle of May" 
     }, 
     { 
      "artist": "Aracy de Almeida", 
      "title": "Passarinho do Relogio (Cuco)" 
     } 
    ], 
    "1941": [ 
     { 
      "artist": "Glenn Miller", 
      "title": "Chattanooga Choo Choo" 
     }, 
     { 
      "artist": "Artie Shaw", 
      "title": "Frenesi" 
     }, 
     { 
      "artist": "Jimmy Dorsey", 
      "title": "Blue Champagne" 
     }, 
     { 
      "artist": "Sammy Kaye", 
      "title": "Daddy" 
     }, 
     { 
      "artist": "Freddy Martin", 
      "title": "Piano Concerto in B Flat" 
     }, 
     { 
      "artist": "Glenn Miller", 
      "title": "Adios" 
     }, 
     { 
      "artist": "Sammy Kaye", 
      "title": "Daddy" 
     }, 
     { 
      "artist": "Artie Shaw", 
      "title": "Blues in the Night" 
     }, 
     { 
      "artist": "Billie Holiday", 
      "title": "God Bless the Child" 
     }, 
     { 
      "artist": "Jimmy Dorsey", 
      "title": "Jim" 
     }, 
     { 
      "artist": "The Andrews Sisters", 
      "title": "I'll Be With You in Apple Blossom Time" 
     }, 
     { 
      "artist": "Duke Ellington", 
      "title": "I Got it Bad & That Ain't Good" 
     }, 
     { 
      "artist": "Horace Heidt", 
      "title": "G'bye Now" 
     }, 
     { 
      "artist": "Freddy Martin", 
      "title": "The Hut Sut Song (A Swedish Serenade)" 
     }, 
     { 
      "artist": "The Ink Spots", 
      "title": "I Don't Want to Set the World On Fire" 
     }, 
     { 
      "artist": "Harry James", 
      "title": "Music Makers" 
     }, 
     { 
      "artist": "Tommy Dorsey & Frank Sinatra", 
      "title": "This Love of Mine" 
     }, 
     { 
      "artist": "The Sons of the Pioneers", 
      "title": "Cool Water" 
     }, 
     { 
      "artist": "Benny Goodman", 
      "title": "Solo Flight" 
     }, 
     { 
      "artist": "Xavier Cugat", 
      "title": "Chica, Chica, Boom, Chic" 
     }, 
     { 
      "artist": "Ernest Tubb", 
      "title": "Walking the Floor Over You" 
     }, 
     { 
      "artist": "Guy Lombardo", 
      "title": "And the Band Played On" 
     }, 
     { 
      "artist": "Vera Lynn", 
      "title": "A Nightingale Sang in Berkeley Square" 
     }, 
     { 
      "artist": "Dick Robertson", 
      "title": "Ferryboat Serenade" 
     }, 
     { 
      "artist": "The Jesters", 
      "title": "It's a Great Day for the Irish" 
     }, 
     { 
      "artist": "The Tic-Toc Rhythm Orchestra", 
      "title": "Ferryboat Serenade" 
     }, 
     { 
      "artist": "Judy Garland", 
      "title": "It's a Great Day for the Irish" 
     }, 
     { 
      "artist": "Francisco Alves", 
      "title": "Canta Brasil" 
     }, 
     { 
      "artist": "George Formby", 
      "title": "Bless 'em All (The Service Song)" 
     }, 
     { 
      "artist": "Al Bowlly", 
      "title": "When That Man is Dead & Gone" 
     }, 
     { 
      "artist": "Bing Crosby", 
      "title": "Shepherd's Serenade" 
     }, 
     { 
      "artist": "Tommy Dorsey", 
      "title": "Do I Worry?" 
     }, 
     { 
      "artist": "Charlie Barnet", 
      "title": "I Hear A Rhapsody" 
     }, 
     { 
      "artist": "Wayne King", 
      "title": "You Are My Sunshine" 
     }, 
     { 
      "artist": "Gene Krupa", 
      "title": "It All Comes Back To Me Now" 
     }, 
     { 
      "artist": "Big Maceo Merriweather", 
      "title": "Worried Life Blues" 
     }, 
     { 
      "artist": "Big Bill Broonzy", 
      "title": "Key to the Highway" 
     }, 
     { 
      "artist": "Joe Loss Orchestra", 
      "title": "'V' Stands For Victory" 
     }, 
     { 
      "artist": "Carlos Galhardo", 
      "title": "Ala Li La" 
     }, 
     { 
      "artist": "Tommy Tucker", 
      "title": "I Don't Want to Set the World On Fire" 
     }, 
     { 
      "artist": "Kay Kyser", 
      "title": "Alexander the Swoose (Half Swan, Half Goose)" 
     }, 
     { 
      "artist": "Al Donahue", 
      "title": "The Wise Old Owl" 
     }, 
     { 
      "artist": "Woody Herman", 
      "title": "Blue Flame" 
     }, 
     { 
      "artist": "Washboard Sam", 
      "title": "She Belongs to The Devil" 
     }, 
     { 
      "artist": "Dorival Caymmi", 
      "title": "O Mar" 
     }, 
     { 
      "artist": "Ray Noble", 
      "title": "By the Light of the Silvery Moon" 
     }, 
     { 
      "artist": "Joel & Gaucho", 
      "title": "Aurora" 
     }, 
     { 
      "artist": "Jay McShann", 
      "title": "Vine Street Blues" 
     } 
    ] 
} 

Я пытаюсь удалить повторяющиеся записи за каждый год. Я пытался, но он не работает:

<?php 

    $decade = file_get_contents("decade.json"); 
    $decade = json_decode($decade, true); 

    foreach($decade as $key => $year) { 
     foreach($year as $k => $item) { 
      $title = $item["title"]; 
      $artist = $item["artist"]; 
      $i = 0; 
      foreach($year as $kkk => $it) { 
       if ($it["title"] = $title) { 
        unset($decade[$key][$kkk]); 
        //echo "found"; 
       } 
       $i = $i + 1; 
      } 
     } 
    } 

    echo json_encode($decade); 
?> 
+1

Сначала, пожалуйста, спросите позже. http://php.net/manual/en/function.array-unique.php – Frank

+0

Возможный дубликат [PHP-массив отражает повторяющиеся значения] (http://stackoverflow.com/questions/28630786/php-array-delect-duplicate-values) –

ответ

1

Метод (немного чрезмерное в отношении проверки «дубликатов») ...

  • Прочитать «источник источника» по году, исполнитель, название.
  • Добавьте его в выходной массив, если нет дубликат записи.

Простой способ проверить, если это дубликат запись является использование in_array на выходном массиве. Мне не нравится, что это будет дорого для больших массивов. Это легко кодировать.

Очень быстрая проверка наличия дублирующей записи в массиве, если вы используете «ключи».

Я решил использовать 'ключи' ...

A 'songSigniture' состоит из:

  • года
  • сепаратора '|'
  • строчные буквы художник
  • separator '|'
  • строчными Название

  • Затем превращают в SHA1 хэш.

Это songSigniture сверяется массива '$ songSignitures'.

Это не много «дополнительного кода», но он не нужен.

Протестировано на: PHP 5.3.29; Working code at Eval.in

$outSongs = array(); 

$outDuplicates = array(); 

// Avoid 'in_array' scans which are expensive for large arrays... need to benchmark it 
// Create a 'signature' of 'year', artist and title as a 'key' so we 
// can check it exists in the array quickly. 

$songSignitures = array(); 

foreach($source as $year => $yearSongs) { // songs by year... 

    foreach ($yearSongs as $songDetails) { // all the songs for one year 

     $artist = $songDetails['artist']; 
     $title = $songDetails['title']; 

     // generate a 'key' that can be checked quickly but is unique. 
     // make it lettercase insensitive. 
     $songSig = sha1($year .'|'. strtolower($artist) .'|'. strtolower($title)); 

     if (!isset($songSignitures[$songSig])) { // add to the output... 
      $songSignitures[$songSig] = true; 
      $outSongs[$year][] = $songDetails; 
     } 
     else { // store duplicates for checking later 
      $outDuplicates[$year][] = $songDetails; 
     } 
    } 
} 

echo '<pre>'; 
print_r($outSongs); 
echo '</pre>'; 

echo '<pre>'; 
print_r($outDuplicates); 
echo '</pre>'; 

/* 
echo '<pre>'; 
print_r($songSignitures); 
echo '</pre>'; 
*/ 
0

Условные вы используете в операторе IF для сравнения строк не является правильным. Вы должны использовать:

($ это [ "название"] === $ название)

Вместо:

($ это [ "название"] = $ название)