Let's make some examples:
array("Paul", "", "Daniel") // false
array("Paul", "Daniel") // true
array("","") // false
What's a neat way to work around this function?
To check if an array contains empty elements call the includes() method on the array, passing it undefined as a parameter. The includes method will return true if the array contains an empty element or an element that has the value of undefined .
To check if an array contains an empty string, call the includes() method passing it an empty string - includes('') . The includes method returns true if the provided value is contained in the array and false otherwise.
To check if an array is null, use equal to operator and check if array is equal to the value null. In the following example, we will initialize an integer array with null. And then use equal to comparison operator in an If Else statement to check if array is null. The array is empty.
✓to check whether an array is empty or not just iterate the elements of the array and compare them with null character '/0'. ✓you can also declare an empty array like this arr[]={}. Then use the sizeof function, if it returns 0 your array is empty.
Try using in_array
:
return !in_array("", array("Paul", "", "Daniel")); //returns false
The answer depends on how you define "empty"
$contains_empty = count($array) != count(array_filter($array));
this checks for empty elements in the boolean sense. To check for empty strings or equivalents
$contains_empty = count($array) != count(array_filter($array, "strlen"));
To check for empty strings only (note the third parameter):
$contains_empty = in_array("", $array, true);
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