24

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?

0

7 Answers 7

38

Try using in_array:

return !in_array("", array("Paul", "", "Daniel")); //returns false
Sign up to request clarification or add additional context in comments.

3 Comments

@stereofrog Take a look at the PHP documentation, unless you pass true as a third parameter, in_array will do a loose comparison, so it'll give the same results with array("Paul", 0, "Daniel"), array("Paul", "", "Daniel") or array("Paul", false, "Daniel"). So if the op only wants to match empty string, he could just pass true as the third parameter.
@stereofrog 0 is actually a number and so it's not an empty string.
@stereofrog @David If you don't want to return false if a 0 is in the array, and only on an empty string, just pass true as the third parameter.
34

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);

1 Comment

Super clever way to save a couple lines of code checking for the existence of values in an array!
8

So array_filter() will remove empty the elements from the array and then return this array.

Comparing the returned array to the original array would not be the same.
Meaning the array contains one or more empty elements.

$array = array("Paul", "", "Daniel");
if($array != array_filter($array)) {
    echo "Array contains empty values.";
}

3 Comments

Explain a bit will ya ?
This is beautiful and straight forward! +1
@ShamseerAhammed So array_filter will remove entities that are "false" (=empty) and return the array back without empty elements at it. Meaning if the data is not equal to the original array, the array contains empty values.
5
function has_empty(array $array)
{
    return count($array) != count(array_diff($array, array('', null, array())));
}

Comments

2

Not exactly the answer to the question, but many visitors come here by searching for a solution to this slightly different case:

Check, if an array ONLY contains empty strings

This would be a simple solution with implode(), that will fit to some needs

function testIfEmpty($array) {
    $flat = implode('', $array);
    return !empty($flat):
}

1 Comment

clever way to check if an array is made of only empty strings!
1
function testEmpty($array) {
  foreach ($array as $element) {
    if ($element === '')
      return false;
  }
  return true;
}

Please check out the comments down below for more information.

3 Comments

Depending on how you define "empty" elements, you might want to look at if (empty($element)) instead of if ($element == ""). empty() treats "", false, 0, null (and probably some more) as empty elements.
@sbrattla I only gave an answer to that precise question, but you're write, your answer is more global.
@Oltatus: Not my intention to correct your suggestion, it was more of an additional solution :-) The empty() function isn't always a good choice, especially if the array would be an array holding true/false values. In that case, all 'false' values would be filtered out.
0

Since I do enjoy me some fancy anonymous functions here is my take on it. Not sure about performance, but here it is:

$filter = array_filter(
        ["Paul", "", "Daniel"],
        static function ($value) {
            return empty($value); // can substitute for $value === '' or another check
        }
    );

return (bool) count($filter);

Logically explained. If anonymous returns true, it means that it found an empty value. Which means the filter array will contain only empty values at the end (if it has any).

That's why the return checks if the filter array has values using count function.

The (bool) type cast is equivalent to return count($filter) === 0.

May you all find the happiness you seek.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.