Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Remove duplicate Array from Multidimensional Array with the key defined

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 175
    Comment on it

    Here we will learn that how we can remove duplicate Array from multidimensional Array according to the key in the Array.
    Lets look at the example below :-

    Here I have initialized one multidimensional Array, now I would like to remove those Array which have same date time in Array like we can see here same dates are 2015-09-09 8:12:00 and 2015-12-10 10:12:00. So I will remove the duplicate Array which have same dates on the basis of key "date"

    <?php
    
    $array = array (
        array (
            "name" => "abc" ,
            "Age" => "25" ,
            "date" => "2015-09-09 8:12:00"
        ) ,
        array (
            "name" => "def" ,
            "Age" => "27" ,
            "date" => "2015-12-10 10:12:00"
        ) ,
        array (
            "name" => "abc" ,
            "Age" => "25" ,
            "date" => "2015-09-09 8:12:00"
        ) ,
        array (
            "name" => "jkl" ,
            "Age" => "22" ,
            "date" => "2015-09-11 12:12:00"
        ) ,
        array (
            "name" => "def" ,
            "Age" => "27" ,
            "date" => "2015-12-10 10:12:00"
        ) ,
    );
    
    echo "Original Array";
    echo "<pre>";
    print_r ( $array );
    echo "</pre>";
    
    function uniqueMultiArray ( $array , $key )
    {
        $temp_array = $key_array = $duplicates = array ();
        $i = 0;
    
        foreach ( $array as $val )
        {
            if ( ! in_array ( $val[ $key ] , $key_array ) )
            {
                $key_array[ $i ] = $val[ $key ];
                $temp_array[ $i ] = $val;
            }
            else
            {
                $duplicates[] = $val[ $key ];
            }
    
            $i ++;
        }
        echo "Duplicate Array";
        echo "<pre>";
        print_r ( $duplicates );
        echo "</pre>";
    
        return $temp_array;
    }
    
    $newArray = uniqueMultiArray ( $array , "date" );
    echo "New Array";
    echo "<pre>";
    print_r ( $newArray );
    echo "</pre>";
    

    Now run the code in your Browser , you will see the below output :-

    Original Array
    Array
    (
        [0] => Array
            (
                [name] => abc
                [Age] => 25
                [date] => 2015-09-09 8:12:00
            )
    
        [1] => Array
            (
                [name] => def
                [Age] => 27
                [date] => 2015-12-10 10:12:00
            )
    
        [2] => Array
            (
                [name] => abc
                [Age] => 25
                [date] => 2015-09-09 8:12:00
            )
    
        [3] => Array
            (
                [name] => jkl
                [Age] => 22
                [date] => 2015-09-11 12:12:00
            )
    
        [4] => Array
            (
                [name] => def
                [Age] => 27
                [date] => 2015-12-10 10:12:00
            )
    
    )
    Duplicate Array
    Array
    (
        [0] => 2015-09-09 8:12:00
        [1] => 2015-12-10 10:12:00
    )
    New Array
    Array
    (
        [0] => Array
            (
                [name] => abc
                [Age] => 25
                [date] => 2015-09-09 8:12:00
            )
    
        [1] => Array
            (
                [name] => def
                [Age] => 27
                [date] => 2015-12-10 10:12:00
            )
    
        [3] => Array
            (
                [name] => jkl
                [Age] => 22
                [date] => 2015-09-11 12:12:00
            )
    
    )
    

    So Now here we can see original Array which I have initialized in the beginning, duplicates Array dates and New Array which have removed those two same dates Array Data.

    Enjoy the Code ! :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: