Checking if all the array items are empty PHP

An older question but thought I'd pop in my solution as it hasn't been listed above.

function isArrayEmpty(array $array): bool {
    foreach($array as $key => $val) {
        if ($val !== '' || $val !== null) // remove null check if you only want to check for empty strings
            return false;
    }
    return true;
}

Implode the array with an empty glue and check the size of the resulting string:

<?php if (strlen(implode($array)) == 0) echo 'all values of $array are empty'; ?>

Please note this is a safe way to consider values like 0 or "0" as not empty. The accepted answer using an empty array_filter callback will consider such values empty, as it uses the empty() function. Many form usages would have to consider 0 as valid answers so be careful when choosing which method works best for you.


You can just use the built in array_filter

If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed.

So can do this in one simple line.

if(!array_filter($array)) {
    echo '<li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>';
}