PHP – How to remove duplicate values from the multidimensional array?

Many times we need to remove duplicate value form the multidimensional array. Let’s see how we can remove duplicate value from the multidimensional array in PHP.

We removed duplication value using array_map the function on PHP let’s see how we can be used the array_map function for removing value from the multidimensional array.

<?php
$array = Array(
    Array(
        'First',
        'Second',
        'Third'
    ),
    Array(
        'Four',
        'Five',
        'Six'
    ),
    Array(
        'First',
        'Second',
        'Third'
    ),
    Array(
        'Four',
        'Five',
        'Six'
    ),
    Array(
        'Four',
        'Seven',
        'Six'
    )
);

$uniqueArray = array_map("unserialize", array_unique(array_map("serialize", $array)));

echo '<pre>';
print_r($uniqueArray);

The output of the unique array is blow

Array
(
    [0] => Array
        (
            [0] => First
            [1] => Second
            [2] => Third
        )

    [1] => Array
        (
            [0] => Four
            [1] => Five
            [2] => Six
        )

    [4] => Array
        (
            [0] => Four
            [1] => Seven
            [2] => Six
        )

)

We removed the duplicate value from the multidimensional array with the losing key.

If you face any issue after using above function write the comment below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s