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

How to remove null values ​​from php array

Apr 25, 2023 pm 06:26 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1279
29
C# Tutorial
1257
24