How to check whether an array is empty using PHP?

If you just need to check if there are ANY elements in the array, you can use either the array itself, due to PHP’s loose typing, or – if you prefer a stricter approach – use count():

if (!$playerlist) {
     // list is empty.
}
if (count($playerlist) === 0) {
     // list is empty.
}

If you need to clean out empty values before checking (generally done to prevent explodeing weird strings):

foreach ($playerlist as $key => $value) {
    if (!strlen($value)) {
       unset($playerlist[$key]);
    }
}
if (!$playerlist) {
   //empty array
}

Leave a Comment