Home Backend Development PHP Problem How to clear empty arrays in two-dimensional arrays in php

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

Apr 19, 2023 am 09:15 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24