How does php clear an empty array from a two-dimensional array

04-10-2024

In PHP array operation, there are often null values in the array, which usually affects the subsequent data processing operations, so it is necessary to clear the null values (empty string, false, null, etc.) in the array. This article will introduce how to clear an empty array in a two-dimensional array.

In PHP, you can use the array_filter function to clear the null values in the array. This function can accept two parameters, the first parameter represents the array to be filtered, and the second parameter represents the callback function to be used. The callback function can specify which values to filter out. For example, if we want to filter out empty strings and false, we can define the callback function as follows:

function my_callback($value){ return $value ! == '' && $value ! == false; }

Where $value represents each element in the array, use! The = = operator determines whether the condition is met. Then use the array_filter function to filter the array:

$arr = array('a', '', 'b', false, null); $new_arr = array_filter($arr, 'my_callback'); print_r($new_arr);

The output result is:

Array( [0] => a [2] => b)

As you can see, empty strings and false are filtered out. But when we are dealing with two-dimensional arrays, the situation becomes a little more complicated.

Consider the following two-dimensional array:

$arr = array( array('a', '', 'b'), array('c', false, null), array('', 'd', 'e'));

Our goal is to get rid of the empty array. Using the array_filter function, you can write the filter conditions as follows:

function my_callback($row){ return array_filter($row, function($value){ return $value ! == '' && $value ! == false; }); }

Where $row represents each row in a two-dimensional array, array_filter is used to filter the elements of each row, and then the result is returned.

Then we can use the array_map function to apply this callback function to each row in the two-dimensional array:

$new_arr = array_map('my_callback', $arr); print_r($new_arr);

The output result is:

Array( [0] => Array ( [0] => a [2] => b ) [1] => Array ( [0] => c ) [2] => Array ( [1] => d [2] => e ))

As you can see, the empty array has been cleared. It should be noted that the result returned by using the array_map function is still a two-dimensional array, and each row may contain a different number of elements, so it may be necessary to use the array_values function to re-index the array.

$new_arr = array_map('my_callback', $arr); $new_arr = array_map('array_values', $new_arr); print_r($new_arr);

The output result is:

Array( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c ) [2] => Array ( [0] => d [1] => e ))

As you can see, the array has been reindexed.

To sum up, clearing empty arrays in two-dimensional arrays can be achieved by combining array_filter and array_map functions, and two callback functions need to be written, one for filtering elements and the other for re-indexing the filtered arrays. The advantage of this method is simple and easy to understand, but the disadvantage is that the code is lengthy. For programs that need frequent array operations, you may need to consider using more efficient algorithms.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us