Last Updated : 29 Jul, 2024
Summarize
Comments
Improve
In this article, we will discuss removing duplicate elements from an array in PHP. We can get the unique elements by using array_unique() function. This function will remove the duplicate values from the array.
Syntax:
array array_unique($array, $sort_flags);
Note: The keys of the array are preserved i.e. the keys of the not removed elements of the input array will be the same in the output array.
Parameters:
This function accepts two parameters that are discussed below:
- $array: This parameter is mandatory to be supplied and it specifies the input array from which we want to remove duplicates.
- $sort_flags: It is an optional parameter. This parameter may be used to modify the sorting behavior using these values:
- SORT_REGULAR: This is the default value of the parameter $sort_flags. This value tells the function to compare items normally (don’t change types).
- SORT_NUMERIC: This value tells the function to compare items numerically.
- SORT_STRING: This value tells the function to compare items as strings.
- SORT_LOCALE_STRING: This value tells the function to compare items as strings, based on the current locale.
Return Value:
The array_unique() function returns the filtered array after removing all duplicates from the array.
Using array_unique() function
The array_unique() function in PHP removes duplicate values from an array by comparing the values and removing subsequent occurrences. It returns a new array with only unique values while preserving the original order of elements. This function is simple and efficient for deduplicating arrays in PHP applications.
Example 1: PHP program to remove duplicate values from the array.
<?php // Input Array$a = array( "red", "green", "red", "blue");// Array after removing duplicatesprint_r(array_unique($a));?>
Output:
Array
(
[0] => red
[1] => green
[3] => blue
)
Example 2: PHP program to remove duplicate elements from an associative array.
<?php // Input array$arr = array( "a" => "MH", "b" => "JK", "c" => "JK", "d" => "OR");// Array after removing duplicatesprint_r(array_unique($arr));?>
Output:
Array
(
[a] => MH
[b] => JK
[d] => OR
)
Example 3: This is another way of removing the duplicates from the array using the PHP in_array() method.
<?php // This is the array in which values// will be stored after removing// the duplicates$finalArray = array();// Input array$arr = array( "a" => "MH", "b" => "JK", "c" => "JK", "d" => "OR");foreach ($arr as $key => $value){ // If the value is already not stored // in the final array if (!in_array($value, $finalArray)) $finalArray[$key] = $value;} // End foreachprint_r($finalArray);?>
Output:
Array
(
[a] => MH
[b] => JK
[d] => OR
)
Using array_filter()
To remove duplicate values from an array using `array_filter()`, apply a callback function that checks each element’s index against its first occurrence. Return true only for the first occurrence to filter out duplicates.
Example: This example shows the use of the above-mentioned approach.
<?php$array = [1, 2, 2, 3, 4, 4, 5];$uniqueArray = array_filter($array, function($value, $key) use ($array) { return array_search($value, $array) === $key;}, ARRAY_FILTER_USE_BOTH);print_r(array_values($uniqueArray));?>
Output
Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5)
Using Iteration and Comparison
To remove duplicate values from an array in PHP without using `array_unique()`, iterate through the array, check each value’s existence in a separate array (`$uniqueArray`), and append it if not already present.
Example: This example shows the use of the above-mentioned approach.
<?phpfunction removeDuplicates($array) { $uniqueArray = []; foreach ($array as $value) { // Check if the value is already in $uniqueArray if (!in_array($value, $uniqueArray)) { $uniqueArray[] = $value; } } return $uniqueArray;}// Example array with duplicate values$array = [1, 2, 2, 3, 4, 4, 5];// Remove duplicates$uniqueArray = removeDuplicates($array);// Print the unique arrayprint_r($uniqueArray);?>
Output
Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5)
Using array_flip() and array_values() Functions:
The array_flip() function in PHP exchanges keys with their associated values in an array, effectively removing duplicate values. `array_values()` is then used to reset the array indices, providing a cleaned-up array without duplicate values while maintaining the original order.
Example
<?php$array = array("apple", "banana", "cherry", "apple", "banana");$uniqueArray = array_values(array_flip($array));print_r($uniqueArray);?>
Output
Array( [0] => 3 [1] => 4 [2] => 2)
Using array_count_values()
The `array_count_values()` function counts all the values of an array and returns an associative array where keys are the original values and values are their counts. To remove duplicates, filter keys with a count of 1, and then use `array_keys()` to extract unique values.
Example:
<?php$array = [1, 2, 2, 3, 4, 4, 5];// Count values$valueCounts = array_count_values($array);// Filter to keep only values that appear once$uniqueValues = array_keys(array_filter($valueCounts, function($count) { return $count === 1;}));print_r($uniqueValues);?>
Output
Array( [0] => 1 [1] => 3 [2] => 5)
Next Article
How to Remove Duplicate Elements from an Array using Lodash ?