Home  >  Article  >  Backend Development  >  How to remove null value from entire array in php

How to remove null value from entire array in php

PHPz
PHPzOriginal
2023-04-26 10:34:40651browse

In PHP, array is a very common and important data structure that can be used to store and process various data. However, in actual development, we often encounter the need to remove certain elements in the array, including null values. Therefore, this article will introduce how to remove null from the entire array in PHP.

  1. Using the array_filter function

The array_filter function is a built-in function in PHP that can filter elements in an array and return a new array after filtering.

Use the array_filter function to remove null values ​​​​in the array. You only need to pass the array as the first parameter of the function and specify an anonymous function as the second parameter to filter. The specific code is as follows:

$array = array("apple", null, "banana", null, "cherry");
$newArray = array_filter($array, function ($value) {
    return $value !== null;
});
print_r($newArray);

The output result after running is:

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

It can be seen from the output result that all null values ​​​​have been removed from the new array, leaving only non-null values.

  1. Using a foreach loop

In addition to using the array_filter function, you can also use a foreach loop to traverse the array one by one and add non-null values ​​one by one to a new array. The specific code is as follows:

$array = array("apple", null, "banana", null, "cherry");
$newArray = array();
foreach ($array as $value) {
    if ($value !== null) {
        $newArray[] = $value;
    }
}
print_r($newArray);

The output result after operation is the same as the above method:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)

Through the above two methods, we can easily remove the null value from the entire array, thereby obtaining a non- A new array of null values.

It should be noted that some special circumstances need to be considered in actual development, such as the presence of other data types in the array, code performance and compatibility issues, etc. Therefore, when using the above methods, you need to make the optimal choice based on the actual situation.

To sum up, this article introduces two methods to remove null from the entire array in PHP: using the array_filter function and using the foreach loop. Both methods have their own advantages and disadvantages, and developers can choose according to actual needs.

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