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

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

  • 14 July, 2018
  • Renish Khunt
  • PHP
  • 83

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.