I'm looking for the most performance friendly approach to check if all values in an array are null or if it at least has one element with other types.
i.e. i need a method called containsOnlyNull($array), which returns booleans according to the passed array
e.g. :
$a = containsOnlyNull([null,null,null,null,null]);
$b = containsOnlyNull([null,null,1,null,null]);
// $a will be true
// $b will be false
To check if all of the values in an array are equal to null , use the every() method to iterate over the array and compare each value to null , e.g. arr. every(value => value === null) . The every method will return true if all values in the array are equal to null .
An array cannot be null. If it's null, then it is not an array: it is null.
To check a variable is null or not, we use is_null() function. A variable is considered to be NULL if it does not store any value. It returns TRUE if value of variable $var is NULL, otherwise, returns FALSE.
function containsOnlyNull($input)
{
return empty(array_filter($input, function ($a) { return $a !== null;}));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With