Home > Article > Backend Development > PHP recursively calls the method of deleting null elements of an array, recursive array_PHP tutorial
This article describes the example of php recursively calling the method of deleting the empty value elements of the array. Share it with everyone for your reference. The details are as follows:
This function can delete all null elements in the array, including empty strings, empty arrays, etc.
function array_remove_empty($arr){ $narr = array(); while(list($key, $val) = each($arr)){ if (is_array($val)){ $val = array_remove_empty($val); // does the result array contain anything? if (count($val)!=0){ // yes :-) $narr[$key] = $val; } } else { if (trim($val) != ""){ $narr[$key] = $val; } } } unset($arr); return $narr; }
Demo example:
Copy code The code is as follows: array_remove_empty(array(1,2,3,'',array(),4)) => returns array(1,2,3,4 )
I hope this article will be helpful to everyone’s PHP programming design.