How to remove duplicate values from a multi-dimensional array in PHP?

Spread the love

Sometimes, we want to remove duplicate values from a multi-dimensional array in PHP.

In this article, we’ll look at how to remove duplicate values from a multi-dimensional array in PHP.

How to remove duplicate values from a multi-dimensional array in PHP?

To remove duplicate values from a multi-dimensional array in PHP, we can use the array_unique and array_map functions.

For instance, we write

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

to write

array_map("serialize", $input)

to map the $input entries to values that can be compared directly.

Then we call array_unique to remove the duplicates.

Next, we call array_map again with 'unserialize' to map the unique values back to their original values.

Conclusion

To remove duplicate values from a multi-dimensional array in PHP, we can use the array_unique and array_map functions.

Leave a Reply

Your email address will not be published. Required fields are marked *