Current Location: Home> Latest Articles> PHP Implementation of Removing Duplicates from a 2D Array: Using array_unique Function

PHP Implementation of Removing Duplicates from a 2D Array: Using array_unique Function

gitbox 2025-07-18

Understanding the array_unique Function

In PHP, the array_unique function removes duplicate values from an array and returns a new array. It is very convenient for handling one-dimensional arrays, but when dealing with a two-dimensional array, array_unique cannot be applied directly. Therefore, we need to implement a custom solution for deduplicating a 2D array. This article will show you how to use array_unique along with other functions to achieve this.

Problem Background

Assume we have a 2D array with multiple subarrays, where each subarray contains some elements, some of which may be duplicates. We want to remove the duplicate subarrays and keep only one of each. Here’s an example:


$fruits = array(
   array("apple", "banana", "orange"),
   array("banana", "grapefruit", "apple"),
   array("kiwi", "grapefruit", "banana")
);

In the above example, the first and second subarrays are identical, so we need to remove one. Similarly, the second and third subarrays are also identical, and one of them should be removed. The final deduplicated result should look like this:


$fruits = array(
   array("apple", "banana", "orange"),
   array("banana", "grapefruit", "apple"),
   array("kiwi", "grapefruit", "banana")
);

Solution

To achieve the deduplication, we can combine the array_unique function with other PHP functions. The steps are as follows:

Converting the 2D Array into a 1D Array

First, we need to convert the 2D array into a 1D array so that we can use array_unique for deduplication. We can use the array_map function and call_user_func_array to do this:


$flattened = call_user_func_array('array_merge', $fruits);

This code uses array_merge to combine all the subarrays into a single 1D array. The call_user_func_array function is used to dynamically call the array_merge function.

Using array_unique for Deduplication

Next, we use the array_unique function to remove duplicates from the 1D array:


$unique = array_unique($flattened);

This code stores the deduplicated result in the $unique variable.

Converting the 1D Array Back into a 2D Array

Finally, after deduplication, we need to convert the 1D array back into a 2D array, which can be done using the array_chunk function:


$chunked = array_chunk($unique, count($fruits[0]));

Using array_chunk, we split the 1D array into subarrays with the same length as the original 2D array’s subarrays. This gives us the final deduplicated 2D array.

Complete Code Example

Here is the complete code example:


$fruits = array(
   array("apple", "banana", "orange"),
   array("banana", "grapefruit", "apple"),
   array("kiwi", "grapefruit", "banana")
);

$flattened = call_user_func_array('array_merge', $fruits);
$unique = array_unique($flattened);
$chunked = array_chunk($unique, count($fruits[0]));

print_r($chunked);

Running this code will output the following result:

Array
(
    [0] => Array
        (
            [0] => apple
            [1] => banana
            [2] => orange
        )

    [1] => Array
        (
            [0] => banana
            [1] => grapefruit
            [2] => apple
        )

    [2] => Array
        (
            [0] => kiwi
            [1] => grapefruit
            [2] => banana
        )
)

Conclusion

By following these steps, we have successfully used the array_unique function to remove duplicates from a 2D array. First, we convert the 2D array to a 1D array, then use array_unique for deduplication, and finally convert the 1D array back to a 2D array. This approach is simple and effective for removing duplicate subarrays.

It’s important to note that array_unique only removes elements with identical values. If you need to remove subarrays with the same structure but different values, you may need to write custom code. I hope this article helps you understand and apply the array_unique function for deduplicating 2D arrays in PHP.