Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking all array values at once

Tags:

arrays

php

Is there a simple way to check if all values in array are equal to each other?

In this case, it would return false:

$array[0] = 'yes';
$array[1] = 'yes';
$array[2] = 'no';

And in this case, true:

$array[0] = 'yes';
$array[1] = 'yes';
$array[2] = 'yes';

So, yeah, is there a function/method to check all array values at once?

Thanks in advance!

like image 578
tomsseisums Avatar asked Sep 30 '10 10:09

tomsseisums


2 Answers

Not a single function, but the same could be achieved easily(?) with:

count(array_keys($array, 'yes')) == count($array)
like image 144
TuomasR Avatar answered Oct 11 '22 11:10

TuomasR


another possible option

if(count(array_unique($array)) == 1)
like image 27
Aaron W. Avatar answered Oct 11 '22 10:10

Aaron W.