How to clear empty array in php

PHPz
Release: 2023-04-27 09:32:19
Original
1063 people have browsed it

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template