如何删除多维数组中的重复元素?

时间:2015-04-14 15:12:44

标签: php arrays multidimensional-array

我有一个像这样的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);
?>

2 个答案:

答案 0 :(得分:1)

方法(关于检查'重复'有点过分了)......

  • 按年份,艺术家,标题阅读“歌曲源阵列”。
  • 如果不是重复条目,则将其添加到输出数组。

检查它是否是重复条目的简单方法是在输出数组上使用 in_array 。我不喜欢它对大型阵列来说会变得昂贵。虽然很容易编码。

如果您使用“密钥”,则可以快速检查数组中是否存在重复条目

我选择使用'钥匙'......

'songSigniture'包括:

  • separator'|'
  • 小写艺术家
  • separator'|'
  • 小写标题

  • 然后转换为 SHA1 哈希。

根据数组'$ songSignitures'检查此 songSigniture

这不是很多“额外代码”,但不需要它。

测试: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>';
*/

答案 1 :(得分:0)

您在IF语句中用于字符串之间比较的条件不正确。你应该使用:

($ it [“title”] === $ title)

而不是:

($ it [“title”] = $ title)