PHP: remove duplicate elements from a multi-dimensional array

This post is over 3 years old, so please keep in mind that some of its content might not be relevant anymore.

array_unique removes duplicate values from an array. Unfortunately it doesn’t work with multi-dimensional arrays.

A quick workaround is to serialize all elements of the first/parent array in order to make it unidimensional, let array_unique doing its job and then unserialize it back.

A one liner for this is:

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

Enjoy!

One thought on “PHP: remove duplicate elements from a multi-dimensional array”

  1. Thank you for sharing your info. I truly appreciate your efforts and
    I am waiting for your further post thanks once again.

Leave a Reply

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