How to delete duplicate elements in a multidimensional array?

fozuse

I have a json file like this. It contains popular songs by year. I will use this songs to create decade based playlists later.

{
    "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"
        }
    ]
}

I am trying to delete duplicate entries in each year. I tried but It does not work:

<?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);
?>
Ryan Vincent

Method (a little excessive as regards checking for 'duplicates')...

  • Read the 'song source array' by year, artist, title.
  • Add it to an output array if not a duplicate entry.

The easy way of checking if it is a duplicate entry is to use in_array on the output array. I don't like that is it will get expensive for large arrays. It is easy to code though.

It is really quick to check if there is a duplicate entry in an array if you are using 'keys'.

I chose to use 'keys'...

A 'songSigniture' consists of:

  • year
  • separator '|'
  • lowercase artist
  • separator '|'
  • lowercase title

  • Then converted to a SHA1 hash.

This songSigniture is checked against the array '$songSignitures'.

It is not a lot of 'extra code' but it isn't needed.

Tested on: 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>';
*/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to delete array elements where there is a duplicate?

From Dev

Delete duplicate elements in an array

From Dev

Delete duplicate rows from Multidimensional Object array [,]?

From Javascript

Delete duplicate elements from an array

From Dev

Remove duplicate combination of elements from multidimensional array

From Dev

Sum multidimensional array elements if they have an duplicate

From Dev

Delete duplicate elements from Array in Javascript

From Dev

How to remove duplicate values from a multidimensional array?

From Dev

How to detect duplicate values in multidimensional associative array?

From Dev

How to remove duplicate entries from multidimensional array?

From Dev

How to reverse duplicate value in multidimensional array

From Dev

How to check duplicate values in multidimensional associative array?

From Dev

How to delete elements from multidimensional array where sum of values equals 0?

From Dev

delete the last two elements from the multidimensional array having a sub array

From Javascript

How to duplicate elements in a js array?

From Dev

How to get duplicate elements in an array

From Java

Algorithm - How to delete duplicate elements in a list efficiently?

From Dev

How to delete an element inside a multidimensional array in PHP?

From Dev

How to delete a column of a multidimensional array in php?

From Dev

Characters duplicate in a multidimensional array

From Dev

How add elements to a multidimensional array in PHP?

From Dev

Powershell how to count all elements in a multidimensional array

From Dev

How To Access Multidimensional Array Elements to Multiply Them

From Dev

How to remove duplicate key value array in multidimensional array

From Dev

How to delete elements from an array?

From Dev

Delete duplicate elements in SQL

From Dev

Delete Duplicates in Multidimensional Array

From Dev

How to delete duplicate values of objects inside array?

From Dev

how to delete duplicate records from array of objects?

Related Related

  1. 1

    How to delete array elements where there is a duplicate?

  2. 2

    Delete duplicate elements in an array

  3. 3

    Delete duplicate rows from Multidimensional Object array [,]?

  4. 4

    Delete duplicate elements from an array

  5. 5

    Remove duplicate combination of elements from multidimensional array

  6. 6

    Sum multidimensional array elements if they have an duplicate

  7. 7

    Delete duplicate elements from Array in Javascript

  8. 8

    How to remove duplicate values from a multidimensional array?

  9. 9

    How to detect duplicate values in multidimensional associative array?

  10. 10

    How to remove duplicate entries from multidimensional array?

  11. 11

    How to reverse duplicate value in multidimensional array

  12. 12

    How to check duplicate values in multidimensional associative array?

  13. 13

    How to delete elements from multidimensional array where sum of values equals 0?

  14. 14

    delete the last two elements from the multidimensional array having a sub array

  15. 15

    How to duplicate elements in a js array?

  16. 16

    How to get duplicate elements in an array

  17. 17

    Algorithm - How to delete duplicate elements in a list efficiently?

  18. 18

    How to delete an element inside a multidimensional array in PHP?

  19. 19

    How to delete a column of a multidimensional array in php?

  20. 20

    Characters duplicate in a multidimensional array

  21. 21

    How add elements to a multidimensional array in PHP?

  22. 22

    Powershell how to count all elements in a multidimensional array

  23. 23

    How To Access Multidimensional Array Elements to Multiply Them

  24. 24

    How to remove duplicate key value array in multidimensional array

  25. 25

    How to delete elements from an array?

  26. 26

    Delete duplicate elements in SQL

  27. 27

    Delete Duplicates in Multidimensional Array

  28. 28

    How to delete duplicate values of objects inside array?

  29. 29

    how to delete duplicate records from array of objects?

HotTag

Archive