Tuesday, February 18, 2014

[PHP][Resolved] check all values in array are equal.

This method is by codaddict in stackoverflow and this post is for providing an example code only:

In the sample, it used the if statement if(count(array_unique($arr)) == 1){} to make checking.

Sample 1:
<?php
$arr = Array("a","a","a");
if(count(array_unique($arr)) == 1){
    echo "all value in array are same";
}else{
    echo "Values in array are not equal";
}
?>
Result:



Sample 2:
<?php
$arr = Array("a","a","b");
if(count(array_unique($arr)) == 1){
    echo "all value in array are same";
}else{
    echo "Values in array are not equal";
}
?>
Result:




Reference:
http://stackoverflow.com/questions/2554410/how-to-check-if-all-values-in-array-are-identical

No comments :

Post a Comment