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
2010-03-31 15:55:42
+5
A:
$results = array_unique($myArray);
if(count($results) == 1){
// $myArray is all duplicates
}
inkedmn
2010-03-31 15:56:25
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
2010-03-31 15:58:13
It doesn't modify the original array (`$myArray`, in this case), rather it returns an array with duplicates removed.
inkedmn
2010-03-31 16:00:26
Or, you can create a new variable with the array in it, and leave the original array alone.
Cryophallion
2010-03-31 16:01:30
@Anthony this function doesn't affect the original array. So $myArray will stay the same.
St. John Johnson
2010-03-31 16:03:22
Ah touche, my mind blanked, +1!
Anthony Forloney
2010-03-31 16:07:27
+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
2010-03-31 15:59:40
`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
2010-03-31 16:11:26
A:
You could also use this check:
count(array_count_values($arr)) == 1
Tom Haigh
2010-03-31 16:12:04