Check if all values in array are the same

前端 未结 8 858
生来不讨喜
生来不讨喜 2020-12-02 22:17

I need to check if all values in an array equal the same thing.

For example:

$allValues = array(
    \'true\',
    \'true\',
    \'true\',
);
         


        
8条回答
  •  情深已故
    2020-12-02 22:35

    If your array contains actual booleans (or ints) instead of strings, you could use array_sum:

    $allvalues = array(TRUE, TRUE, TRUE);
    if(array_sum($allvalues) == count($allvalues)) {
        echo 'all true';
    } else {
        echo 'some false';
    }
    

    http://codepad.org/FIgomd9X

    This works because TRUE will be evaluated as 1, and FALSE as 0.

提交回复
热议问题