How to Check an Array Contains only Unique Values in PHP?
In PHP, verifying that an array contains unique values is a common task. This involves ensuring that each element in the array appears only once.
There are multiple methods to achieve this check effectively:
Table of Content
Using array_unique
() and count() function
To check if an array contains unique values in PHP, the function compares the count of the array with the count of the array after applying array_unique. If both counts match, all values are unique.
Example: The example shows how to Check an Array Containing Unique Values in PHP using array_unique
and count
.
function isArrUni($array) {
if (count($array) === count(array_unique($array))) {
echo "The array " . json_encode($array) .
" contains unique values.\n";
} else {
echo "The array " . json_encode($array) .
" does not contain unique values.\n";
}
}
$array = [1, 2, 3, 4, 5];
isArrUni($array);
$array = [1, 2, 3, 4, 5, 5];
isArrUni($array);
Output:
The array [1,2,3,4,5] contains unique values. The array [1,2,3,4,5,5] does not contain unique values.
Using array_count_values() function
To check if an array contains unique values, use array_count_values
to count occurrences of each value. If any count is greater than 1, the array does not contain unique values.
Example: The example shows how to Check an Array Containing Unique Values in PHP using array_count_values
.
function isArrUni($array) {
$valueCounts = array_count_values($array);
foreach ($valueCounts as $count) {
if ($count > 1) {
echo "The array " . json_encode($array)
. " does not contain unique values.\n";
return;
}
}
echo "The array " . json_encode($array) .
" contains unique values.\n";
}
$array = [1, 2, 3, 4, 5];
isArrUni($array);
$array = [1, 2, 3, 4, 5, 5];
isArrUni($array);
Output:
The array [1,2,3,4,5] contains unique values. The array [1,2,3,4,5,5] does not contain unique values.
Using array_flip() function
To check if an array contains unique values, use array_flip
to swap keys and values. If the count of the flipped array matches the original, the array contains unique values.
Example: The example shows how to Check an Array Containing Unique Values in PHP using array_flip
.
function isArrUniq($array) {
if (count($array) === count(array_flip($array))) {
echo "The array " . json_encode($array)
. " contains unique values.\n";
} else {
echo "The array " . json_encode($array)
. " does not contain unique values.\n";
}
}
$array = [1, 2, 3, 4, 5];
isArrUniq($array);
$array = [1, 2, 3, 4, 5, 5];
isArrUniq($array);
Output:
The array [1,2,3,4,5] contains unique values. The array [1,2,3,4,5,5] does not contain unique values.
Using a Loop with a HashSet
Another approach to verify if an array contains unique values is by using a loop in combination with a HashSet-like structure (an associative array in PHP). This method involves iterating through the array and keeping track of seen values. If a value is encountered more than once, the array does not contain unique values.
Example: The following example demonstrates how to check if an array contains unique values using a loop and an associative array as a set.
<?php
function hasUniqueValues($array) {
$seen = [];
// Iterate through each element in the array
foreach ($array as $value) {
if (isset($seen[$value])) {
return false;
}
$seen[$value] = true;
}
return true;
}
$array1 = [1, 2, 3, 4, 5];
$array2 = [1, 2, 3, 4, 5, 5];
if (hasUniqueValues($array1)) {
echo "The array [1, 2, 3, 4, 5] contains unique values.\n";
} else {
echo "The array [1, 2, 3, 4, 5] does not contain unique values.\n";
}
if (hasUniqueValues($array2)) {
echo "The array [1, 2, 3, 4, 5, 5] contains unique values.\n";
} else {
echo "The array [1, 2, 3, 4, 5, 5] does not contain unique values.\n";
}
?>
<?php
function hasUniqueValues($array) {
$seen = [];
// Iterate through each element in the array
foreach ($array as $value) {
if (isset($seen[$value])) {
return false;
}
$seen[$value] = true;
}
return true;
}
$array1 = [1, 2, 3, 4, 5];
$array2 = [1, 2, 3, 4, 5, 5];
if (hasUniqueValues($array1)) {
echo "The array [1, 2, 3, 4, 5] contains unique values.\n";
} else {
echo "The array [1, 2, 3, 4, 5] does not contain unique values.\n";
}
if (hasUniqueValues($array2)) {
echo "The array [1, 2, 3, 4, 5, 5] contains unique values.\n";
} else {
echo "The array [1, 2, 3, 4, 5, 5] does not contain unique values.\n";
}
?>
Output
The array [1, 2, 3, 4, 5] contains unique values. The array [1, 2, 3, 4, 5, 5] does not contain unique values.