开发者

How to check if all values in array are identical?

In PHP how c开发者_如何学JAVAan I quickly tell if all values in array are identical?


You can use the test:

count(array_unique($arr)) == 1;

Alternatively you can use the test:

$arr === array_fill(0,count($arr),$arr[0]);


$results = array_unique($myArray);
if(count($results) == 1){
   // $myArray is all duplicates
}


You could also use this check:

count(array_count_values($arr)) == 1


$myArray = array('1','1','1');
$results = array_unique($myArray);
if(count($results) == 1)
{
    echo"all value is duplicates";
}
else
{
    echo"all value is not duplicates";
}


You can check for count(array_intersect($arr1, $arr2)) == 0


Do a test run and see if all results are the same:

foreach ($array as $newarray){
    echo $newarray. '';
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜