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

Php 如何删除多维数组中的重复元素?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个类似这样的json文件。它每年都有流行歌曲。稍后我将使用这些歌曲创建基于十年的播放列表 { "1940": [ { "artist": "Tommy Dorsey", "title": "I'll Never Smile Again" }, { "artist": "Bing Crosby", "title": "Only Forever

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

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

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

而不是:

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

方法(在检查“重复项”时有点过度)

  • 按年份、艺术家、标题阅读“歌曲源阵列”
  • 如果不是重复条目,则将其添加到输出数组中
检查它是否是重复条目的简单方法是在输出数组上使用
in_array
。我不喜欢这一点,因为对于大型阵列来说,这会变得很昂贵。不过,编写代码很容易

如果您使用的是“keys”,那么检查数组中是否存在重复条目的速度非常快

我选择使用“钥匙”

“歌曲签名”包括:

  • 分隔符“|”
  • 小写艺术家
  • 分隔符“|”
  • 小写标题

  • 然后转换为SHA1散列

根据数组“$SongSignatures”检查此SongSignatures

虽然没有太多的“额外代码”,但并不需要

测试环境:PHP5.3.29

$outSongs=array();
$outDuplicates=array();
//避免对大型阵列进行昂贵的“阵列内”扫描。。。需要对其进行基准测试
//创建“年”、“艺术家”和“头衔”的“签名”作为“钥匙”,以便我们
//可以快速检查它是否存在于阵列中。
$songsigniters=array();
foreach($sourceas$year=>$yearSongs){//songs by year。。。
foreach($yearSongs作为$songDetails){//一年的所有歌曲
$artist=$songDetails['artist'];
$title=$songDetails['title'];
//生成可以快速检查但唯一的“密钥”。
//使其不区分大小写。
$songSig=sha1($year.'|').strtolower($artist.'|'.strtolower($title));
如果(!isset($songsigniters[$songSig]){//添加到输出。。。
$songsigniters[$songSig]=真;
$outSongs[$year][]=$songDetails;
}
else{//存储副本以备以后检查
$outDuplicates[$year][]=$songDetails;
}
}
}
回声';
印刷品(外松);
回声';
回声';
打印(复印件);
回声';
/*
回声';
印刷(签名);
回声';
*/
先用谷歌搜索,然后再问。可能重复的
$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>';
*/