Remove duplicate values from array in PHP

Remove duplicate values from array using PHP

Posted by Lukee Beeno on April 11, 2023

Remove duplicate values from array in PHP

Remove duplicate values from array using PHP

Posted by Lukee Beeno on April 11, 2023

Let us look at a few way in which you can remove duplicate values in PHP.

Option 1. Use a for loop

$arr=[5,11,6,8,2,5];
$newArr = [];

for($i=0; $i<count($arr); $i++) {  
   if (!in_array($arr[$i], $newArr)) {
      array_push($newArr, $arr[$i]);
   }
}
var_dump($newArr); //[5,11,6,8,2]

In this example, we have an array named $arr that contains some duplicate values. We create a new array named $newArr and loop through each value in the $arr using the foreach loop. Inside the loop, we use the in_array() function to check if the current value already exists in the $newArr. If the value is not found in the $newArr, we add it to the end of the array using the array_push($newArr, $arr[$i]) syntax.

Finally, we use the print_r() function to output the contents of the $newArr, which should contain only the unique values from the original array.

Option 2. Use array_unique() method

The array_unique() function takes an array as its argument and returns a new array with all duplicate values removed. The function compares the values using a "loose" comparison, which means that it considers values of different types as equal if their string representation is the same.

$arr=[5,11,6,8,2,5];

$newArr = array_unique($arr);
var_dump($newArr); //[5,11,6,8,2]

In this example, we have an array of numbers, which contains one duplicate value (5). We pass this array to the array_unique() function, which returns a new array with the duplicate removed. The resulting array is stored in the $newArr variable.

Option 3. Use array_flip() with array_values() method

The array_flip() function exchanges all keys with their associated values in an array, effectively creating a new array where the values become keys and the keys become values. However, if the original array contains duplicate values, only the last value will be preserved in the flipped array. This is because array keys must be unique, so when multiple values map to the same key, only the last one is kept.

The array_values() function returns all the values of an array as a new array, with the keys renumbered starting from zero.

$arr=[5,11,6,8,2,5];

$newArr = array_values( array_flip( array_flip( $arr )));

var_dump($newArr); //[5,11,6,8,2]

In this example, we have an array of numbers, which contains one duplicate value (5). We first use array_values() to create a new array with the same values as the original array, but with renumbered keys. We then use array_flip() to flip the keys and values of the array, effectively removing any duplicates. Finally, we use array_flip() again to renumber the keys of the flipped array, giving us a new array with only unique values.

This field is required
Your question have been successfully submitted and will be reviewed before published
Please login to ask a question