Home >Backend Development >PHP Tutorial >PHP recursively calls the method of deleting null elements from an array_PHP Tutorial

PHP recursively calls the method of deleting null elements from an array_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 16:54:02838browse

php recursively calls the method of deleting the empty value element of the array

This article describes the example of php recursively calling the method of deleting the array null value element. 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.

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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;

}

1

2

3

4

5 6

78 9 10 11 12 13
14
15
16 17 18 19 20
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; }
Demonstration example: 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. http://www.bkjia.com/PHPjc/991651.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/991651.htmlTechArticlePHP recursively calls the method to delete the null value element of the array. This article describes the method of php recursively calling the method to delete the null value element of the array. . Share it with everyone for your reference. The details are as follows: This function can...
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