How to clear empty arrays in two-dimensional arrays in php

PHPz
Release: 2023-04-19 09:27:44
Original
702 people have browsed it

In PHP array operations, we often encounter null values in the array, which usually affects subsequent data processing operations. Therefore, it is necessary to clear the null values (empty string, false, null, etc.) in the array. Lose. This article will introduce how to clear empty arrays in two-dimensional arrays.

In PHP, you can use the array_filter function to clear empty 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 like this:

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

where $value represents each element in the array, using the !== operation Determine 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);
Copy after login

The output result is:

Array ( [0] => a [2] => b )
Copy after login

As you can see, empty strings and false are filtered out. But when we're dealing with two-dimensional arrays, things get a little more complicated.

Consider the following two-dimensional array:

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

Our goal is to clear the empty array. Using the array_filter function, you can write the filter conditions in the following form:

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

where $row represents each row in the two-dimensional array, use array_filter to filter the elements of each row, and then return the result.

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);
Copy after login

The output result is:

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

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 the array may need to be re-indexed using the array_values function.

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

The output result is:

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

You can see that the array has been re-indexed.

To summarize, clearing empty arrays in two-dimensional arrays can be achieved by combining the array_filter and array_map functions. You need to write two callback functions, one for filtering elements and one for re-indexing the filtered array. . The advantage of this method is that it is simple and easy to understand, but the disadvantage is that the code is relatively lengthy. For programs that require frequent array operations, you may want to consider using a more efficient algorithm.

The above is the detailed content of How to clear empty arrays in two-dimensional arrays in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!