Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save azizultex/ea1633e641e5614be326f3424b7e9326 to your computer and use it in GitHub Desktop.

Select an option

Save azizultex/ea1633e641e5614be326f3424b7e9326 to your computer and use it in GitHub Desktop.
Clean a multidimensional array from null, 0, empty string
/* https://rogerpadilla.wordpress.com/2009/08/21/remove-empty-items-from-array/ */
function array_non_empty_items($input) {
if (!is_array($input)) {
return $input;
}
$non_empty_items = array();
foreach($input as $key => $value) {
if ($value) {
$non_empty_items[$key] = array_non_empty_items($value);
}
}
// added array_filter azizultex
return array_filter($non_empty_items);
}
/* USED in viz360 at superdraft */
$fields = array('service_360vid', 'service_3d', 'service_gallery', 'service_interactive', 'service_panora');
$values_in_array = array();
foreach($fields as $field) {
$field_value = get_post_meta(get_the_ID(), $field, true);
$filtered = array_non_empty_items($field_value);
if ($filtered) {
$values_in_array[$field] = $filtered;
}
}
var_dump($values_in_array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment