check if two arrays contain same elements php code example
Example 1: php get intersection of two arrays
$array1 = [1, 2];
$array2 = [2, 3, 4];
$commonValue = array_intersect($array1, $array2);
$array1 = [1, 2];
$array2 = [2, 3, 4];
$arrayOfArrays = [$array1, $array2];
$commonValue = array_intersect(...$arrayOfArrays);
Example 2: php check if all values in array are equal
$allvalues = array('true', 'true', 'true');
if (count(array_unique($allvalues)) === 1 && end($allvalues) === 'true') {
}
Example 3: php check if all array values are the same
if (count(array_unique($allvalues)) === 1 && end($allvalues) === 'true') {
}
if (in_array('false', $allvalues, true)) {
}
Example 4: check if any values are the same in an array php
if(count(array_unique($array, SORT_REGULAR)) < count($array)) {
} else {
}