When working with multi-dimensional arrays, it can be necessary to eliminate duplicate values to ensure data integrity. Here's an efficient approach to achieve this in PHP.
We can leverage the array_map function to apply the serialize function to each array in the multi-dimensional array. Serializing converts each array to a unique string representation. Then, array_unique eliminates duplicate string representations. Finally, we unserialize the unique strings back into their corresponding arrays.
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Let's consider an example multi-dimensional array.
$input = [ [0 => 'abc', 1 => 'def'], [0 => 'ghi', 1 => 'jkl'], [0 => 'mno', 1 => 'pql'], [0 => 'abc', 1 => 'def'], [0 => 'ghi', 1 => 'jkl'], [0 => 'mno', 1 => 'pql'], ];
After applying the above code, the duplicate values are removed, resulting in:
[ [0 => 'abc', 1 => 'def'], [0 => 'ghi', 1 => 'jkl'], [0 => 'mno', 1 => 'pql'], ]
The above is the detailed content of How to Efficiently Remove Duplicate Arrays from a Multi-Dimensional Array in PHP?. For more information, please follow other related articles on the PHP Chinese website!