Home  >  Article  >  Backend Development  >  How to remove null values ​​from array in php

How to remove null values ​​from array in php

PHPz
PHPzOriginal
2023-04-23 09:13:122074browse

In PHP development, it is often necessary to operate on arrays, and removing null values ​​​​from arrays is one of the most common operations. The most common situation is that when obtaining data from a form, some data may be left blank by the user, but in subsequent processing, these blank values ​​will affect the normal operation of the program. Therefore, we need to remove these null values.

This article will introduce several methods of removing empty values ​​from arrays in PHP.

Method 1: Use array_filter() function

array_filter() is a built-in function in PHP that can filter elements in an array. We can pass a callback function as a parameter, and the callback function returns a Boolean value. When it is true, the element is retained, when it is false, the element is deleted. If no callback function is passed, all null values, 0, false, and null in the array are deleted.

The sample code is as follows:

$arr = array('a', '', 'b', null, 'c', ' ', 'd', false);
$arr = array_filter($arr, 'strlen');
print_r($arr);

The output result is as follows:

Array
(
    [0] => a
    [2] => b
    [4] => c
    [6] => d
)

In the above code, we use the strlen function as the callback function, which returns the length of a string, False if the length of the string is 0. Therefore, all elements with non-zero length are retained.

Method 2: Use a foreach loop

Another common way to remove null values ​​from an array is to use a foreach loop to traverse the array and delete the null values. The sample code is as follows:

$arr = array('a', '', 'b', null, 'c', ' ', 'd', false);

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

print_r($arr);

The output result is the same as method one:

Array
(
    [0] => a
    [2] => b
    [4] => c
    [6] => d
)

In the above code, we use the empty() function to determine whether the element is empty, and if it is empty, use unset( ) function deletes the element.

Method 3: Use the array_diff() function

There is also a method to use the array_diff() function to remove null values ​​​​in the array. The array_diff() function is a PHP built-in function used to calculate the difference of an array. When we calculate the null value of an array as a "difference set" with another empty array, we can get the original array after removing the null value.

The sample code is as follows:

$arr = array('a', '', 'b', null, 'c', ' ', 'd', false);
$arr = array_diff($arr, array(''));

print_r($arr);

The output result is the same as the above two methods:

Array
(
    [0] => a
    [2] => b
    [4] => c
    [6] => d
)

In the above code, we use an empty array as a parameter, this empty array There are no elements, so its difference is the result of the original array minus the null values.

Summary

This article introduces three methods of removing empty values ​​from arrays in PHP: using the array_filter() function, using the foreach loop, and using the array_diff() function. These methods can effectively remove null values ​​​​in the array and make the program more stable. In actual development, we can choose a method that suits us according to specific needs.

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