tags:

views:

92

answers:

3

ie, verify

$a[0]=1; $a[0]=1; $a[0]=1; $a[0]=1; $a[0]=1; $a[0]=1; $a[0]=1; $a[0]=1;

but not

$a[0]=1; $a[0]=2; $a[0]=1; $a[0]=1; $a[0]=1; $a[0]=1; $a[0]=1; $a[0]=1;

thanks :)

+13  A: 
count(array_unique($a)) == 1;
Wrikken
+1. Nice and concise.
richsage
Wow. I'm not a great fan of PHP (it works well, but is not very beautiful), but it does have some handy functions. Just the one significance needed, in this case.
MvanGeest
To nipick, it should be `count(array_unique($a)) <= 1`; if the array has no elements, then the proposition "all the elements are the same" is true.
Artefacto
MvanGeest - i find that happens often in php! Wrikken - thanks - elegant solution!
significance
more formally, the proposition "exists some value `A` such as for any value `B` in the array `ARR`, `A=B`" is true if `ARR` is empty.
Artefacto
+2  A: 

Check if all items are equal to the first item:

$first = $array[0];
foreach ($array as $a) {
    if ($a != $first) {
        return false;
    }
}
return true;
Sjoerd
+1  A: 

If you are new to PHP, then it might be easier for you to use it in this way

function chkArrayUniqueElem($arr) {
    for($i = 0; $i < count($arr); $i++) {
        for($j = 0; $j < count($arr); $j++) {
            if($arr[$i] != $arr[$j]) return false;
        }
    }
    return true;
}

Other variants brought up earlier are more simple in use.

Eugene
How on earth is that easier for a beginner?
Well IMHO it is. At least when I started learning it was. You can see every move in this function. Nothing passes you.
Eugene