search
HomeBackend DevelopmentPHP ProblemHow to clear empty arrays in two-dimensional arrays in php

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;
}

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);

The output result is:

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

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')
);

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;
    });
}

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);

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 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);

The output result is:

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

    [1] => Array
        (
            [0] => c
        )

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

)

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
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

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools