Last active
August 19, 2016 05:09
-
-
Save azizultex/ea1633e641e5614be326f3424b7e9326 to your computer and use it in GitHub Desktop.
Clean a multidimensional array from null, 0, empty string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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