Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for empty nested array

Tags:

php

I have the following array:

array(4) {
  [29] => NULL
  [31] => NULL
  [33] => NULL
  [35] => NULL
}

I would like to test all keys if all keys contain NULL values.

like image 663
Christian-G Avatar asked Jul 21 '26 01:07

Christian-G


2 Answers

if(count(array_filter($input, 'is_null')) == count($input)) { 

}

should be what you are looking for :)

like image 168
Andreas Wong Avatar answered Jul 23 '26 15:07

Andreas Wong


// need php version >= 5.3 or you need to define a function, or just use a loop to check.
if (!count(array_filter($your_array, function($var){return $var !== null}))) {
  // all values is null.
}
like image 42
xdazz Avatar answered Jul 23 '26 13:07

xdazz



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!