How to Remove Duplicate Values From Array in PHP

Last Updated on June 28, 2021 by Roshan Parihar

In this tutorial, learn how to remove duplicate values from array in PHP. The short answer is to use the array_unique() of PHP and pass the array variable as its argument.

You can delete or eliminate the identical values from an indexed array, associative array, and multidimensional array. So, let’s find out the different methods with the examples given below here.

Method 1: Using array_unique() to Remove Duplicate Values From Array in PHP

In this method, you have to use the array_unique() function of PHP to remove the duplicate values.

Removing from Indexed Array in PHP

To remove duplicate values from the indexed array, you have to use the array_unique() and pass the indexed array variable as its argument.

Output

The indexed array after removing duplicate elements is: Array ( [0] => Cycle [1] => Bike [2] => Car [4] => Bolero )

The example given above contains the indexed array variable with duplicate elements. After that, the output gives the indexed array with unique values.

Removing from Associative Array in PHP

Similarly, you can remove the duplicate values from associative using the array_unique() function. The function takes a single argument as an associative array variable. See the example below to learn the method.

Output

The associative array after removing duplicate elements is: Array ( [Cycle] => 2 [Bike] => 5 [Car] => 9 )

The output given above shows an associative array with unique values.

Method 2: Using Foreach Loop to Delete Duplicate Values From Array in PHP

The foreach loop of PHP can also be used to delete the duplicate values or elements from an array in PHP. Let’s find out below the separate example for indexed array and associative array.

Deleting from Indexed Array in PHP

To use the foreach loop to delete the duplicate elements from an indexed array, you have to first declare an empty array. After that, use the foreach loop inside which you have to use the if condition and check the array using the in_array() function.

Further, the unique elements get added to the empty array variable on each iteration. See the example given below to learn the method.

Output

The indexed array after removing duplicate elements is: Array ( [0] => Cycle [1] => Bike [2] => Car [4] => Bolero )

The above example resulted in the indexed array with unique elements in it.

Deleting from Associative Array in PHP

Similarly, to delete the duplicate elements from an associative array, you can use the foreach loop. It also requires declaring an empty array. After that, use the foreach loop with if condition to check the array using the in_array() inside it.

Likewise, on each iteration of foreach loop, the unique values get added to the array.

Output

The associative array after removing duplicate elements is: Array ( [Cycle] => 2 [Bike] => 5 [Car] => 9 )

The above example shows the output that contains the unique associative array elements.

You can use any of the methods given above to eliminate identical values from indexed and associative arrays in PHP. However, foreach loop takes some time to give the result. The array_unique() function gives a faster result.

How to Delete Identical Elements from Multidimensional Array in PHP

In addition to the above methods, you can also use the array_unique() function and pass two arguments to it. The first argument is the multidimensional array variable. The second argument is the SORT_REGULAR keyword.

As a result, you will get a multidimensional array with unique elements.

Output

The multidimensional array after removing duplicate elements is: Array ( [0] => Array ( [Cycle] => 2 [Bike] => 5 [Car] => 9 ) [1] => Array ( [Cycle] => 3 [Bike] => 6 [Car] => 10 ) [2] => Array ( [Cycle] => 5 [Bike] => 8 [Car] => 12 ) )

So, the result shows the mutlidimensional array with unique array elements.

To sum up, you can use the array_unique() to get the unique elements from an array in PHP. However, the multidimensional array requires to use the SORT_REGULAR keyword as the second argument of array_unique().

You May Also Like to Read

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.