tags:

views:

183

answers:

5

how can I quickly tell if all values in array are identical?

+1  A: 

do a test run and see if the results are all the same

foreach ($array as $newarray){
    echo $newarray. '';
}
ggfan
+5  A: 
$results = array_unique($myArray);
if(count($results) == 1){
   // $myArray is all duplicates
}
inkedmn
Although this is a valid answer, I would also note that `array_unique` *removes* the duplicates, so this approach works if he does not need the array with all it's values.
Anthony Forloney
It doesn't modify the original array (`$myArray`, in this case), rather it returns an array with duplicates removed.
inkedmn
Or, you can create a new variable with the array in it, and leave the original array alone.
Cryophallion
@Anthony this function doesn't affect the original array. So $myArray will stay the same.
St. John Johnson
Ah touche, my mind blanked, +1!
Anthony Forloney
+3  A: 

You can use the test:

count(array_unique($arr)) == 1;

Alternatively you can use the test:

$arr === array_fill(0,count($arr),$arr[0]);
codaddict
`array_unique` doesn't actually modify the original array. Please take a look at the PHP Manual: http://php.net/manual/en/function.array-unique.php
St. John Johnson
@John: Thanks for the useful comment. I've edited my ans.
codaddict
A: 

You could also use this check:

count(array_count_values($arr)) == 1
Tom Haigh
A: 

Why not to just loop over this array?

Col. Shrapnel