Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether an array is empty without using a loop?

Tags:

arrays

php

isset

Is there any function available in PHP to check whether an array is empty or how can I do this without using loop?

For example: $b = array('key1' => '', 'key2' => '', 'key3' => '', 'key4' => '');

How can I check array $b contains empty values without using a loop?

like image 754
Paulraj Avatar asked Apr 26 '26 11:04

Paulraj


1 Answers

Simple:

function allEmpty($array)
{
    return empty(array_filter($array)); // (PHP < 5.3) or
    $array = array_filter($array); return empty($array); // (PHP >= 5.3) or just
    return array_filter($array) === array();
}

function someEmpty($array)
{
    return ($array !== array_filter($array));
}
like image 192
Alix Axel Avatar answered Apr 29 '26 01:04

Alix Axel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!