Star
Embed
What would you like to do?
PHP : Check if array has duplicate values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
❮ PHP Array Reference
Example
Remove duplicate values from an array:
<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>
Try it Yourself »
Definition and Usage
The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.
Note: The returned array will keep the first array item's key type.
Syntax
array_unique(array, sorttype)
Parameter Values
array | Required. Specifying an array |
sorttype | Optional. Specifies how to compare the array elements/items. Possible values:
|
Technical Details
Returns the filtered array |
4.0.1+ |
PHP 7.2: If sorttype is SORT_STRING, this returns a new array and adds the unique elements. PHP 5.2.9: The default value of sorttype was changed to SORT_REGULAR. PHP 5.2.1: The default value of sorttype was changed back to SORT_STRING. |
❮ PHP Array Reference
Check if all Values in an Array are Equal #
To check if all values in an array are equal:
- Use the Array.every() method to iterate over the array.
- Check if each array element is equal to the first one.
- The every method only returns true if the condition is met for all array elements.
Copied!
const arr1 = [1, 1, 1]; const arr2 = [1, 1, 2]; function allAreEqual(array) { const result = array.every(element => { if (element === array[0]) { return true; } }); return result; } console.log(allAreEqual(arr1)); // 👉️ true console.log(allAreEqual(arr2)); // 👉️ falseThe function we passed to the Array.every method gets called with each element in the array until it returns a falsy value or iterates over the entire array.
If the function returns a falsy value, the every method short-circuits and returns false.
On each iteration, we check if the current element is equal to the element at position 0. If the condition is met for all elements, then they are all equal.
When the every method is called on an empty array it returns true for any condition.
Copied!
const arr1 = []; function allAreEqual(array) { const result = array.every(element => { if (element === array[0]) { return true; } }); return result; } console.log(allAreEqual(arr1)); // 👉️ trueWhether you need to handle this depends on your use case. If consider an empty array as an array where all values are equal, you don't need to do anything.
However, if you consider an empty array one where not all elements are equal, use the following solution.
Copied!
const arr1 = []; const arr2 = [1, 1, 1]; function allAreEqual(array) { if (array.length > 0) { const result = array.every(element => { if (element === array[0]) { return true; } }); return result; } return false; } console.log(allAreEqual(arr1)); // 👉️ false console.log(allAreEqual(arr2)); // 👉️ trueWe first check for the array's length and we only call the every method if the array contains at least 1 element.
This is entirely use case specific. For example, you might want to check if the array has at least 2 elements to return true from the function.
An alternative approach is to use a Set object.
Check if all Values in an Array are Equal using Set #
To check if all values in an array are equal:
- Pass the array to the Set constructor and access the size property.
- The Set object only stores unique values.
- If the Set has a length of 1, then all array elements are equal or the array only contains 1 element.
Copied!
The Set object allows us to store unique values and removes all duplicates automatically.
If we pass it an array containing the same value multiple times, it would only get added once to the Set.
Copied!
console.log(new Set([1, 1, 1])); // 👉️ { 1 }The size property of the Set allows us to get the number of values stored in the Set.
If the number of values in the Set is equal to 1, then all of the values in the array are equal or the array only contains one element.
If you only want to return true if the array contains at least 2 elements, add the following if statement.
Copied!
const arr1 = [1]; const arr2 = [1, 1, 1]; function allAreEqual(array) { if (array.length > 1) { const result = new Set(array).size === 1; return result; } else { return false; } } console.log(allAreEqual(arr1)); // 👉️ false console.log(allAreEqual(arr2)); // 👉️ trueWe first make sure the array contains more than 1 element. If it does, we pass the array to the Set constructor.
In all other cases, we return false.