Home >Backend Development >PHP Problem >How to remove null values ​​​​in an array in php

How to remove null values ​​​​in an array in php

zbt
zbtOriginal
2023-08-04 16:58:491618browse

php method to remove empty values ​​​​in an array: 1. Use the array_filter() function to determine whether the array element is empty through a callback function. If the callback function returns true, the elements will be retained in the new array; Otherwise, it is removed; 2. Use a foreach loop to traverse the array and remove null values ​​by judging whether the element is empty; 3. Use the array_diff() function to compare the original array with an array containing only null values Compare to get an array filtered with null values.

How to remove null values ​​​​in an array in php

The operating environment of this tutorial: windows10 system, php8.1.3 version, DELL G3 computer.

In PHP development, we often need to handle array operations. One of the common operations is to remove null values ​​​​from the array. Null values ​​may be caused by user input, database queries, or other operations. This article will introduce several methods to remove null values ​​​​in arrays.

Method 1: Use the array_filter() function

The array_filter() function can filter out the null values ​​​​in the array and return a new array. It uses a callback function to determine whether the array element is empty. If the callback function returns true, the element is retained in the new array; otherwise, it is removed. The following is a sample code that uses the array_filter() function to remove null values ​​​​in an array:

$array = array("apple", "", "banana", null, "cherry", 0, "date");
$filteredArray = array_filter($array, function($value) {
return $value !== '' && $value !== null;
});
print_r($filteredArray);
?>

The output result is:

Array ( [0] => apple [2] => banana [4] => cherry [6] => date 
)

In the above code, the callback function determines whether the array element is An empty string or null to determine whether the element remains in the new array.

Method 2: Use foreach loop

We can also use foreach loop to traverse the array and remove null values ​​by determining whether the element is empty. The following is a sample code that uses a foreach loop to remove null values ​​​​in an array:

$array = array("apple", "", "banana", null, "cherry", 0, "date");
$filteredArray = array();
foreach($array as $value) {
if($value !== '' && $value !== null) {
$filteredArray[] = $value;
}
}
print_r($filteredArray);
?>

The output result is:

Array ( [0] => apple [2] => banana [4] => cherry [6] => date 
)

In the above code, we create an empty array $filteredArray, and then Use a foreach loop to iterate over the original array $array. If the element is not empty, the element is added to the new array $filteredArray.

Method 3: Use array_diff() function

The array_diff() function is used to calculate the difference set of arrays. We can compare the original array with an array containing only null values ​​to get a null-filtered array. The following is an example code that uses the array_diff() function to remove null values ​​​​in an array:

$array = array("apple", "", "banana", null, "cherry", 0, "date");
$emptyArray = array("", null);
$filteredArray = array_diff($array, $emptyArray);
print_r($filteredArray);
?>

The output result is:

Array ( [0] => apple [2] => banana [4] => cherry [6] => date 
)

In the above code, we create an array that only contains null values The array $emptyArray, and then use the array_diff() function to calculate the difference between the original array $array and $emptyArray, thereby obtaining the array $filteredArray with filtered null values.

Summary

This article introduces three methods to remove null values ​​​​in PHP arrays. Whether you choose to use the array_filter() function, foreach loop or array_diff() function, you can filter null values ​​in the array. Choosing appropriate methods to operate arrays based on actual needs can improve code readability and performance efficiency. .

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