Home  >  Article  >  Backend Development  >  PHP recursively calls the method of deleting null elements of an array, recursive array_PHP tutorial

PHP recursively calls the method of deleting null elements of an array, recursive array_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:55:30839browse

php recursively calls the method of deleting the empty value elements of the array, recursive array

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/991806.htmlTechArticlephp recursively calls the method to delete the empty value element of the array, recursive array. This article tells the example of php recursive call to delete the empty value of the array. Element methods. Share it with everyone for your reference. The details are as follows:...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn