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

How to clear empty array in php

PHPz
PHPzOriginal
2023-04-19 11:37:38762browse

In PHP development, we often need to clear arrays with empty values. Because when processing form submissions, database queries, etc., we cannot allow null values ​​to occupy the array, otherwise it will affect the normal operation of the program. Therefore, it is very necessary to clear the array with empty values. So, how to clear an array with empty values?

1. Use foreach loop to traverse the array

First, we can use foreach loop to traverse the array, and then use the unset function to delete elements with empty values. For example:

foreach ($arr as $key => $value) {
    if (empty($value)) {
        unset($arr[$key]);
    }
}

In this code, we traverse the array $arr through a foreach loop, and use the empty function to determine whether the array element is empty. If the element is empty, use the unset function to delete the element. This clears all empty elements in the array.

2. Use the array_filter function to filter the array

In addition to using the foreach loop to traverse the array, we can also use the PHP built-in function array_filter to filter the array. The array_filter function can filter the array according to the specified callback function and return the filtered array. If the callback function returns true, the element will be retained, if it returns false, the element will be deleted. For example:

$arr = array_filter($arr, function($value) {
    return !empty($value);
});

In this code, we pass in a callback function that determines whether the element is empty. If the element is not empty, it returns true and the element will be retained, otherwise it returns false and the element will be deleted. Finally returns the filtered array.

3. Use the array_walk function to clear arrays with empty values

In addition to using foreach and array_filter, we can also use the array_walk function to clear arrays with empty values. The array_walk function can apply a user-defined function to each element of an array. For example:

array_walk($arr, function(&$value, $key) {
    if (empty($value)) {
        unset($arr[$key]);
    }
});

In this code, we use the array_walk function to traverse the array and pass in a callback function. The callback function checks whether the array element is empty, and if it is empty, uses the unset function to delete the element. It should be noted that we use reference passing (&$value), so that the value of the array element can be modified.

4. Use recursive functions to clear empty elements of multi-dimensional arrays

If the array is a multi-dimensional array, we can use recursive functions to clear empty elements. For example:

function clearEmptyValue($arr) {
    foreach($arr as $key => $value) {
        if(is_array($value)) {
            $arr[$key] = clearEmptyValue($value);
        } elseif(empty($value)) {
            unset($arr[$key]);
        }
    }
    return $arr;
}

In this code, we define a recursive function clearEmptyValue. This function will traverse the array. If the array element is of array type, it will call this function recursively to clear the empty elements. Otherwise, it will determine whether the element is empty and delete it. Finally returns the cleared array.

Summary

The above are several ways to clear arrays with empty values. We can choose an appropriate method to clear based on the actual business scenario. It should be noted that when clearing array elements, it is best to use unset instead of assigning a value to null or '', because these two methods do not actually delete the array elements, but just set the value to empty.

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