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

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

PHPz
Release: 2023-03-28 17:53:41
Original
2144 people have browsed it

In PHP development, when an array contains empty values ​​(i.e. null), we need to remove them so that we can better process other data. This article will introduce you to several methods to remove null values ​​​​in PHP arrays.

1. Use the array_filter function

The array_filter function is a very convenient function in PHP. It can filter out elements in the array that do not meet the conditions, and we can pass Set the callback function method to remove elements whose return value is false.

We can use the following code to remove null values ​​​​in the array:

$array = array("hello", "world", null, "", " ");
$array = array_filter($array, function($value) {
    return !is_null($value) && !empty($value);
});
Copy after login

After the above code operation, we will successfully remove the null values ​​​​in the array and only retain non-empty elements.

2. Use the array_map function

The array_map function is also a commonly used function in PHP. It can traverse the array and perform an operation on each element. We can first define a callback function, then use the array_map function to apply the function to each element in the array, and then put the non-empty elements into another new array.

The following is a sample code:

$array = array("hello", "world", null, "", " ");
$array = array_map(function($value) {
    return $value !== '' && $value !== null ? $value : null;
}, $array);
$array = array_filter($array, function($value) {
    return !is_null($value);
});
Copy after login

The above code will first apply each element in the $array array to the callback function for operation, and then put the non-empty elements into a new array, and finally remove the null values ​​in the new array, thereby achieving the purpose of removing the null values ​​in the array.

3. Use for loop to traverse the array

Another method is to use PHP’s for loop to traverse the array, and then put the non-empty elements into another array . Although this method seems more cumbersome, if you understand the principle, you can easily remove null values ​​​​in the array.

The following is a sample code:

$array = array("hello", "world", null, "", " ");
$newArray = array();
for($i = 0; $i < count($array); $i++) {
    if(!is_null($array[$i]) && $array[$i] !== '') {
        $newArray[] = $array[$i];
    }
}
Copy after login

Here we use PHP's for loop to traverse the array, and then determine whether the element is a null value (null or empty string). If it is not a null value, then put it into a new array.

Summary

In PHP, there are many ways to remove null values ​​​​from arrays. The above three methods are just a few of them. You can choose the method that suits you to achieve the purpose of removing null values ​​​​in the array.

The above is the detailed content of How to remove null values ​​​​from an array in php. For more information, please follow other related articles on the PHP Chinese website!

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