How to convert null value in array in php

PHPz
Release: 2023-04-23 10:28:37
Original
1053 people have browsed it

In PHP, arrays are a very common data type, and there may be some null values ​​in these arrays. If we want to process these arrays, sometimes we need to convert these null values ​​into other values. So how to implement null value conversion in an array in PHP? Let’s introduce it below.

1. What is null value?

In PHP, null is a special value, which means no value and empty. If the value of a variable is null, then the variable is empty, that is, it does not contain any value.

In the array, if the value of a key-value pair is null, it means that the key-value pair does not contain any value.

For example:

$arr = array(
    "key1" => null,
    "key2" => "value2",
    "key3" => null
);
Copy after login

2. Why should you convert null values ​​into other values?

In PHP, sometimes we need to process key-value pairs in an array. For example, we may need to convert the null value in the array into an empty string or other default value. In this case, we need to use some specific methods.

3. How to convert the null value in the array into other values?

In PHP, you can use some built-in functions or custom functions to convert null values ​​in the array. Let’s introduce them separately below.

1. Use the PHP built-in function array_filter()

The array_filter() function in PHP can filter the elements in the array and return a new array that does not contain elements with specified conditions. When the filter condition is null, all null values ​​in the array are filtered out.

For example:

$arr = array(
    "key1" => null,
    "key2" => "value2",
    "key3" => null
);
$new_arr = array_filter($arr);
print_r($new_arr);
Copy after login

The output result is:

Array(
    [key2] => value2
)
Copy after login

As can be seen from the above example, the array_filter() function can be used to filter out the null values ​​​​in the array and return values ​​that do not contain A new array of null values.

2. Use the PHP built-in function array_map()

The array_map() function in PHP can apply all array elements to the specified callback function and return the result as a new array.

We can customize a callback function to convert the null value into the default value. The following example:

$arr = array(
    "key1" => null,
    "key2" => "value2",
    "key3" => null
);
$new_arr = array_map(function($value) {
    return $value ?? ''; //将null值转换成空字符串
}, $arr);
print_r($new_arr);
Copy after login

The output result is:

Array(
    [key1] => 
    [key2] => value2
    [key3] => 
)
Copy after login
Copy after login

As can be seen from the above example, using the array_map() function can convert the null value in the array into the value we want.

3. Custom function

In PHP, we can also customize a function to convert the null value in the array.

For example, we can customize a function null_to_str() to convert the null value in the array into an empty string. The following example:

function null_to_str($arr) {
    foreach ($arr as $key => $value) {
        if (is_null($value)) {
            $arr[$key] = '';
        }
    }
    return $arr;
}

$arr = array(
    "key1" => null,
    "key2" => "value2",
    "key3" => null
);
$new_arr = null_to_str($arr);
print_r($new_arr);
Copy after login

The output result is:

Array(
    [key1] => 
    [key2] => value2
    [key3] => 
)
Copy after login
Copy after login

As you can see from the above example, you can also use a custom function to convert the null value in the array into the value we want.

4. Summary

In PHP, there are many ways to convert null values ​​in an array into other values. We can use PHP's built-in functions array_filter(), array_map(), etc., or we can customize a function to perform conversion. Different conversion methods have different uses and need to be selected according to the actual situation.

If we need to filter or convert the null values ​​​​in the array, we can refer to the method introduced above for processing. The converted new array can meet our needs for array use.

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

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