How to remove null values ​​from php array

PHPz
Release: 2023-04-25 18:45:17
Original
723 people have browsed it

In PHP, array is a very commonly used data type, which can store multiple values ​​and facilitate traversal and operation. However, in actual development, we often encounter situations where arrays contain null values. These null values ​​will bring inconvenience to our program and cause errors when performing certain operations. Therefore, we need to learn how to remove null values ​​from php arrays.

1. What is a null value php array?

In PHP, null values ​​refer to NULL, false, 0, '' (empty string) and unassigned elements in the array. Therefore, a null php array is when the array contains null values.

For example, the following array $arr contains empty values ​​such as NULL, false, 0 and ''.

$arr = array('apple', NULL, 'banana', false, 0, '', 'orange');
Copy after login

2. How to remove null values ​​from php array?

In PHP, you can use the array_filter() function to remove empty php arrays. The array_filter() function can filter array elements using a custom function or callback function. When using this function, array elements that are false, NULL, 0, or the empty string'' will be deleted.

The following is a simple code example:

$arr = array('apple', NULL, 'banana', false, 0, '', 'orange');
$new_arr = array_filter($arr);
print_r($new_arr);
Copy after login

Output result:

Array
(
    [0] => apple
    [2] => banana
    [6] => orange
)
Copy after login
Copy after login

As you can see, the $new_arr array only contains non-empty elements.

3. Use custom functions to remove empty value php arrays

In addition to using the array_filter() function, we can also use custom functions to remove empty value php arrays. The following is a sample code using a custom function:

function removeEmptyElement($arr){
    foreach($arr as $key => $value){
        if(empty($value)){
            unset($arr[$key]);
        }
    }
    return $arr;
}

$arr = array('apple', NULL, 'banana', false, 0, '', 'orange');
$new_arr = removeEmptyElement($arr);
print_r($new_arr);
Copy after login

Output result:

Array
(
    [0] => apple
    [2] => banana
    [6] => orange
)
Copy after login
Copy after login

As you can see, through the custom function removeEmptyElement(), we also successfully removed all elements in the array NULL value.

4. Conclusion

In actual development, it is very common to encounter empty php arrays. This article explains how to use the array_filter() function and custom functions to remove null values ​​from php arrays. I hope this article can help PHP programmers solve practical problems and improve programming efficiency.

The above is the detailed content of How to remove null values ​​from php array. 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