Programming

Recursively remove empty elements and subarrays from a multi-dimensional array

With the help of array_map() and array_filter(), empty array elements can be removed recursively  from the multidimensional array .

array_map ( callable $callback , array $array1 [, array $… ] ) :
array_map() returns an array containing the results of applying the callback function to the corresponding index of array used as arguments for the callback.

array_filter ( array $array [, callable $callback [, int $flag = 0 ]] ) :

array_filter, filters elements of an array using a callback function. If no callback is supplied, all entries of array equal to FALSE will be removed.

Following code is helpful to remove the empty array from multi-dimensional array.

However, array_filter is sufficient to remove the empty array element from single dimensional array.

To study more about array_map() and array_filter(), please check phpnet.

Hits: 19

Standard