Skip to content

Instantly share code, notes, and snippets.

@jbdelhommeau
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbdelhommeau/53c02a10c616678673ca to your computer and use it in GitHub Desktop.
Save jbdelhommeau/53c02a10c616678673ca to your computer and use it in GitHub Desktop.
Recursive empty
$field1 = array(
array('field_14' => null, 'field_21' => array(array('field_22' => null, 'field_23' => null, 'field_24' => null)), 'field_19' => null, 'field_20' => null)
);
$field2 = array();
$field3 = array('field' => 'test');
$field4 = 'coucou';
function is_empty($field) {
$isEmpty = empty($field);
if($isEmpty || !is_array($field)) {
return $isEmpty;
}
return array_reduce($field, function($acc, $value){
if ($acc === false) {
return false;
}
return (is_array($value)) ? is_empty($value) : empty($value);
});
}
var_dump(is_empty($field1));
var_dump(is_empty($field2));
var_dump(is_empty($field3));
var_dump(is_empty($field4));
##RESULT
bool(true)
bool(true)
bool(false)
bool(false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment