Home >Backend Development >PHP Problem >php remove null values ​​from array

php remove null values ​​from array

PHPz
PHPzOriginal
2023-05-19 11:18:372810browse

During the PHP development process, we often encounter situations where we need to process arrays. When processing arrays, a very common need is to remove null values ​​​​in the array. So, how to remove null values ​​​​in an array in PHP? This article will introduce three methods to remove null values ​​​​in arrays.

Method 1: Using a loop to traverse the array

Looping through the array is the most common method of processing arrays. We can use a foreach loop to traverse the array, and then use conditional statements to determine whether each element in the array is a null value. If it is a null value, use the unset function to delete it. The specific steps are as follows:

// 原始数组
$arr = array('foo', '', 'bar', ' ', null, 'baz');

// 遍历数组并删除空值
foreach ($arr as $key => $value) {
    if ($value === '' || $value === ' ' || $value === null) {
        unset($arr[$key]);
    }
}

// 输出处理后的数组
print_r($arr);

Run the above code, the output result is as follows:

Array
(
    [0] => foo
    [2] => bar
    [5] => baz
)

Method 2: Use the array_filter function to filter the array

PHP provides a function called array_filter, Can be used to filter values ​​in an array. This function will pass each element in the array to a callback function for judgment. If the element meets the conditions, it will return true, otherwise it will return false. Finally, the function returns a new array containing only elements that meet the criteria.

We can use an anonymous function as a callback function, and then pass the array as a parameter to the array_filter function. The specific steps are as follows:

// 原始数组
$arr = array('foo', '', 'bar', ' ', null, 'baz');

// 过滤数组中的空值
$new_arr = array_filter($arr, function($value) {
    return ($value !== '' && $value !== ' ' && $value !== null);
});

// 输出处理后的数组
print_r($new_arr);

Run the above code, the output result is as follows:

Array
(
    [0] => foo
    [2] => bar
    [5] => baz
)

Method 3: Use the array_diff function to find the difference set

PHP provides a function called array_diff , can be used to find the difference between two arrays. We can take the difference between the original array and an array containing only null values, and the result is the array after removing the null values. The specific steps are as follows:

// 原始数组
$arr = array('foo', '', 'bar', ' ', null, 'baz');

// 以空值作为差集的基准求差集
$new_arr = array_diff($arr, array('', ' ', null));

// 输出处理后的数组
print_r($new_arr);

Run the above code, the output result is as follows:

Array
(
    [0] => foo
    [2] => bar
    [5] => baz
)

In summary, we have introduced three methods to remove null values ​​​​in PHP arrays, which are: using loop traversal Arrays, use the array_filter function to filter the array, and use the array_diff function to find the difference. In actual development, different methods can be selected according to specific needs.

The above is the detailed content of php remove null values ​​from array. 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