Home Backend Development PHP Problem How to clear empty array in php

How to clear empty array in php

Apr 27, 2023 am 09:11 AM

In PHP, array is one of the common data structures that can store a set of ordered data. In practical applications, we often encounter situations where we need to clear null values ​​​​in an array. This article will introduce several methods to clear empty arrays.

Method 1: Use array_filter()

PHP provides a built-in function array_filter(), which can filter out false values ​​in the array, including empty values ​​(null, false, "" , 0, "0", [], etc.). The usage of this function is as follows:

$array = array("a", "", "b", null, false, 0, "0", []);
$result = array_filter($array);
print_r($result);
Copy after login

The result is as follows:

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

In the above example, we first define an array $array, containing some null values. Then we use the array_filter() function to filter the array, save the filtered results to the $result variable, and print it out. As you can see, all null values ​​are filtered out and only non-null values ​​are retained.

Method 2: Use foreach loop

In addition to using the built-in function array_filter(), we can also use loop statements to clear null values ​​​​in the array. The specific implementation is as follows:

$array = array("a", "", "b", null, false, 0, "0", []);
foreach ($array as $key => $value) {
    if (empty($value)) {
        unset($array[$key]);
    }
}
print_r($array);
Copy after login

In the above example, we use the foreach loop to traverse the array to determine whether each element is empty. If it is empty, use the unset() function to delete the element. Finally, print out the array after deletion. The output is the same as above.

One thing to note is that we use the unset() function to delete null values ​​during the loop, which may change the key names of the elements in the array. If you need to retain the original key names, you need to use the array_values() function to reset the index of the array after the deletion operation.

Method 3: Custom function

Another common way is to define a custom function to clear the null values ​​in the array. The implementation code is as follows:

function clean_array($arr) {
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            $arr[$key] = clean_array($value);
        } else {
            if (empty($value)) {
                unset($arr[$key]);
            }
        }
    }
    return $arr;
}
$array = array("a", "", "b", null, false, 0, "0", []);
$result = clean_array($array);
print_r($result);
Copy after login

In the above code, we define the clean_array() function, which uses recursive method to traverse the array elements and determine whether each element is empty. If If it is empty, use the unset() function to delete the element. If the element is an array, then the function is called recursively to continue clearing null values. Finally, the array after deletion is returned.

Summary

This article introduces several common methods of clearing empty arrays. The most common way is to use the PHP built-in function array_filter(), which can filter out all null values ​​at once. In addition, we can also use the foreach loop statement to traverse the array, determine whether each element is empty, and then use the unset() function to delete the empty value. Finally, we can also define a custom function to clear empty values. This function uses a recursive method to traverse the array elements, determine whether each element is empty, and then use the unset() function to delete the element. Finally, the purpose of clearing the empty array is achieved.

The above is the detailed content of How to clear empty array 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
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24