check if all values in array are equal php
snippet in php

check if all values in array are equal php

user125

if(count(array_unique($array)) === 1) {
    // all values in $array are the same
} else {
    // at least 1 value in $array is different
}

check if any values are the same in an array php

user3744

if(count(array_unique($array, SORT_REGULAR)) < count($array)) {
    // $array has duplicates
} else {
    // $array does not have duplicates
}

how to check if all values in an array are equal php

user689

1. Check if all values are equal without knowing the values from array:
$array = array('true', 'true', 'true');
if((count(array_unique($array)) === 1)) {
  echo "all equal";
} else {
  echo "not equal";
}

2. Check if all values are equal when you know the value from array:
- In this case we know the equal value should be "true" 
$array = array('true', 'true', 'true');
if (count(array_unique($array)) === 1 && end($array) === 'true') {
}