How to Check an Array Contains only Unique Values in PHP?
Last Updated :
16 Jul, 2024
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:
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
.
PHP
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
.
PHP
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
.
PHP
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
<?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";
}
?>
OutputThe array [1, 2, 3, 4, 5] contains unique values.
The array [1, 2, 3, 4, 5, 5] does not contain unique values.
Similar Reads
How to Check if an Array Field Contains a Unique Value or Another Array in MongoDB? MongoDB is a widely used NoSQL database that enables developers to query complex data structures flexibly. In MongoDB, arrays are commonly used to store lists of values, providing flexibility in data modeling. To ensure data integrity and get valuable insights, it's important to understand how to ch
4 min read
How to filter out the non-unique values in an array using JavaScript ? In JavaScript, arrays are the object using the index as the key of values. In this article, let us see how we can filter out all the non-unique values and in return get all the unique and non-repeating elements. These are the following ways by which we can filter out the non-unique values in an arra
4 min read
How to check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP
2 min read
How to check foreach Loop Key Value in PHP ? In PHP, the foreach loop can be used to loop over an array of elements. It can be used in many ways such asTable of ContentUsing the for-each loop with simple valuesUsing the foreach loop with Key-Value pairsUsing array_keys Function to Access Keys and ValuesUsing array_walk Function for IterationUs
3 min read
How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte
3 min read