A recursive function, it will remove all empty values ββand empty arrays from the input array:
//clean all empty values from array function cleanArray($array) { if (is_array($array)) { foreach ($array as $key => $sub_array) { $result = cleanArray($sub_array); if ($result === false) { unset($array[$key]); } else { $array[$key] = $result; } } } if (empty($array)) { return false; } return $array; }
I tested it in this example, it works no matter how deep the array is:
$array = array( 'name' => array( 'name1' => array(0 => 1), 'name2' => array(0 => 3, 1 => array(5 => 0, 1 => 5)), 'name3' => array(0 => '') ) );
Hope this helps :)
source share