3

我有以下数组:

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

如果所有键都包含 NULL 值,我想测试所有键。

4

4 回答 4

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

}

应该是你要找的:)

于 2012-06-07T10:02:29.070 回答
2
// 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.
}
于 2012-06-07T09:57:46.180 回答
0

超级简单的方法:

function allNULL($array){
  foreach($array as $i)
   if($i!=null) 
    return FALSE;
  return TRUE;
}
于 2012-06-07T10:05:52.220 回答
0
<?php
    $filternull = function( $value ) {
       return $value !== null;
    }

    $remaining = array_filter( $yourarray, $filternull );

    echo count( $remaining ); 
    // === 0, if all were "null";
?>
于 2012-06-07T10:10:08.540 回答