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.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php //Declare an indexed array in PHP $myarrayind = array("Cycle", "Bike", "Car", "Cycle", "Bolero", "Car"); //Remove duplicate indexed array elements using array_unique() $RemDupArrInd = array_unique($myarrayind); echo "The indexed array after removing duplicate elements is: "; //Print result print_r($RemDupArrInd); ?> |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php //Declare an associative array in PHP $myarrayassoc = array("Cycle" => 2, "Bike" => 5, "Cycle" => 2, "Car" => 9, "Bike" => 5); //Remove duplicate associative array elements using array_unique() $RemDupArrAssoc = array_unique($myarrayassoc); echo "The associative array after removing duplicate elements is: "; //Print result print_r($RemDupArrAssoc); ?> |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php //Declare an indexed array in PHP $myarrayind = array("Cycle", "Bike", "Car", "Cycle", "Bolero", "Car"); //Deleting duplicate indexed array elements using Foreach loop $RemDupArrInd = array(); $i=0; foreach ($myarrayind as $indvalue){ if(!in_array($indvalue, $RemDupArrInd)) $RemDupArrInd[$i]=$indvalue; $i++; } echo "The indexed array after removing duplicate elements is: "; //Print result print_r($RemDupArrInd); ?> |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php //Declare an associative array in PHP $myarrayassoc = array("Cycle" => 2, "Bike" => 5, "Cycle" => 2, "Car" => 9, "Bike" => 5); //Deleting duplicate associative array elements using Foreach loop $RemDupArrAssoc = array(); foreach ($myarrayassoc as $assockey => $assocvalue){ if(!in_array($assocvalue, $RemDupArrAssoc)) $RemDupArrAssoc[$assockey]=$assocvalue; } echo "The associative array after removing duplicate elements is: "; //Print result print_r($RemDupArrAssoc); ?> |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php //Declare an indexed array in PHP $mymultiarray = array( array("Cycle" => 2, "Bike" => 5, "Car" => 9), array("Cycle" => 3, "Bike" => 6, "Car" => 10), array("Cycle" => 5, "Bike" => 8, "Car" => 12), array("Cycle" => 3, "Bike" => 6, "Car" => 10), array("Cycle" => 2, "Bike" => 5, "Car" => 9), array("Cycle" => 3, "Bike" => 6, "Car" => 10), array("Cycle" => 5, "Bike" => 8, "Car" => 12), array("Cycle" => 3, "Bike" => 6, "Car" => 10), array("Cycle" => 2, "Bike" => 5, "Car" => 9) ); //Remove duplicate multidimensional array elements using array_unique() $RemDupArrMulti = array_unique($mymultiarray, SORT_REGULAR); echo "The multidimensional array after removing duplicate elements is: "; //Print result print_r($RemDupArrMulti); ?> |
Output
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