How to Remove Duplicate Values from a Multidimensional Array in PHP?

Problem:

You have a multidimensional array which has duplicate values in it. You want to remove those duplicate values and make the array a unique one.

Solution:

To remove duplicate entry from a multidimensional array, follow the steps below-

Step 1: Convert the multidimensional array to one-dimensional array
To convert the multidimensional array to a one-dimensional array, first generate byte stream representation of all the elements (including nested arrays) inside the array. serialize() function can generate byte stream representation of a value. To generate byte stream representation of all the elements, call serialize() function inside array_map() function as a callback function. The result will be a one dimensional array no matter how many levels the multidimensional array has.

Step 2: Make the values unique
To make this one dimensional array unique, use array_unique() function.

Step 3: Revert it to the multidimensional array
Though the array is now unique, the values looks like byte stream representation. To revert it back to the multidimensional array, use unserialize() function.

Example:

<pre>
<?php
$appointments = array(
                      array(“Monday”, “5:30 PM”),
                      array(“Friday”, “6:00 PM”),
                      array(“Monday”, “5:30 PM”),
                      array(“Tuesday”, “4:15 PM”),
                      array(“Wednesday”, “8:30 PM”),
                      array(“Thursday”, “1:45 PM”)
);

$one_dimension = array_map(“serialize”, $appointments);
$unique_one_dimension = array_unique($one_dimension);
$unique_multi_dimension = array_map(“unserialize”, $unique_one_dimension);

print_r($unique_multi_dimension);
?>
</pre>

[wpdm_file id=48] Output:

Array
(
    [0] => Array
        (
            [0] => Monday
            [1] => 5:30 PM
        )
    [1] => Array
        (
            [0] => Friday
            [1] => 6:00 PM
        )
    [3] => Array
        (
            [0] => Tuesday
            [1] => 4:15 PM
        )
    [4] => Array
        (
            [0] => Wednesday
            [1] => 8:30 PM
        )
    [5] => Array
        (
            [0] => Thursday
            [1] => 1:45 PM
        )
)

How it works:

Line 3 $appointments is a multidimensional array. Inside it, the first and the third arrays are identical. We’ll remove the third one.
Line 12 array_map() function applies the serialize() function(which is the call back function) to all the elements of the $appointments array. As a result, byte stream representations of all the elements are generated and those are stored in the $one_dimensional array. Here, $one_dimensional is a one dimensional array.
Line 13 array_unique() function removes the duplicate elements from the $one_dimensional array  and makes it unique and store it in $unique_one_dimension array.
Line 14 array_map() function applies the unserialize() function to all the elements of the $unique_one_dimension array. As a result, byte stream representations of all the array elements are converted to the multidimensional array and it is stored in $unique_multi_dimension array.
Line 16 The line prints the unique multidimensional array.

The code (shorter version)
We can write the above code in short as follows-

<pre><php
$appointments = array(
                     array("Monday", "5:30 PM"),
                     array("Friday", "6:00 PM"),
                     array("Monday", "5:30 PM"),
                     array("Tuesday", "4:15 PM"),
                     array("Wednesday", "8:30 PM"),
                     array("Thursday", "1:45 PM")
);

$unique_multi_dimension = array_map("unserialize", array_unique(array_map("serialize", $appointments)));

print_r($unique_multi_dimension);
?></pre>

[wpdm_file id=49]

Output:
Same as above.