Home >Backend Development >PHP Problem >How to delete null value in array in php

How to delete null value in array in php

PHPz
PHPzOriginal
2023-04-19 10:04:33744browse

PHP is a widely used scripting language that is widely used in the field of web development. In development, we often need to operate arrays. When we encounter an array containing null values, we may need to delete it. This article will introduce how to delete null values ​​​​in an array using PHP.

1. Traverse and delete

The simplest way is to traverse and delete null values. First we create an array containing null values.

$arr = array(1, 2, null, 4, null, 6);
print_r($arr);

Output result:

Array
(
    [0] => 1
    [1] => 2
    [2] => 
    [3] => 4
    [4] => 
    [5] => 6
)

We can traverse the array through a foreach loop and use unset() to delete elements containing null values.

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

Output result:

Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [5] => 6
)

Use the unset() function to delete elements in the array, and the is_null() function is used to determine whether it is null, so you can quickly delete null elements in the array.

2. array_filter() function

Another method is to use the PHP built-in function array_filter(). This function accepts an array and returns a new array. It filters the values ​​in the original array through the specified callback function and returns the new array. If the callback function returns true, the element will be retained, and if it returns false, the element will be deleted.

Therefore, we can create an anonymous function to determine whether the element is null and use the array_filter() function to filter.

$arr = array(1, 2, null, 4, null, 6);
$new_arr = array_filter($arr, function($value){
    return $value !== null;
});
print_r($new_arr);

Output result:

Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [5] => 6
)

3. array_diff() function

You can also use the array_diff() function to perform array difference operations to remove null value elements from the original array Delete in. This method requires a difference operation between the original array and an array containing only null values, and the result is a new array that does not contain null values.

$arr = array(1, 2, null, 4, null, 6);
$null_arr = array(null);
$new_arr = array_diff($arr, $null_arr);
print_r($new_arr);

Output result:

Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [5] => 6
)

4. array_map() function

Finally, we can use the array_map() function to filter array null values. This function accepts an array and returns a new array. The values ​​in the original array are processed through the specified callback function and the new array is returned.

Use the array_map() function to convert null values ​​to any values ​​and then filter them out. Here we convert the null value to an empty string and then filter it.

$arr = array(1, 2, null, 4, null, 6);
$new_arr = array_map(function($value){
    return $value ?? '';
}, $arr);
print_r($new_arr);

Output result:

Array
(
    [0] => 1
    [1] => 2
    [2] => 
    [3] => 4
    [4] => 
    [5] => 6
)

Use the ?? operator to convert the null value to an empty string, and then use the array_filter() function to filter to get a new array that does not contain null values.

This article introduces 4 methods to delete null values ​​​​in PHP arrays, namely traversal deletion, array_filter() function, array_diff() function, and array_map() function. Different methods are suitable for different scenarios. When developing, you can choose the method that suits you according to the actual situation.

The above is the detailed content of How to delete null value in 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