Reputation: 189
In PHP how can I quickly tell if all values in array are identical?
Upvotes: 8
Views: 15365
Reputation: 3091
$myArray = array('1','1','1');
$results = array_unique($myArray);
if(count($results) == 1)
{
echo"all value is duplicates";
}
else
{
echo"all value is not duplicates";
}
Upvotes: 0
Reputation: 2492
Do a test run and see if all results are the same:
foreach ($array as $newarray){
echo $newarray. '';
}
Upvotes: -1
Reputation: 57845
You could also use this check:
count(array_count_values($arr)) == 1
Upvotes: 3
Reputation: 455380
You can use the test:
count(array_unique($arr)) == 1;
Alternatively you can use the test:
$arr === array_fill(0,count($arr),$arr[0]);
Upvotes: 45
Reputation: 28245
$results = array_unique($myArray);
if(count($results) == 1){
// $myArray is all duplicates
}
Upvotes: 14