Check if all values in array are the same

前端 未结 8 854
生来不讨喜
生来不讨喜 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:50

    $alltrue = 1;
    foreach($array as $item) {
        if($item!='true') { $alltrue = 0; }
    }
    if($alltrue) { echo("all true."); }
    else { echo("some false."); }
    

    Technically this doesn't test for "some false," it tests for "not all true." But it sounds like you're pretty sure that the only values you'll get are 'true' and 'false'.

提交回复
热议问题