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

How to remove null values ​​from php array

藏色散人
藏色散人Original
2020-11-01 13:45:116955browse

How to remove null values ​​from a php array: First create a PHP sample file; then filter the null values ​​in the array through the "array_filter($entry)" method; and finally print the result through print.

How to remove null values ​​from php array

Recommended: "PHP Video Tutorial"

0, null, false in PHP filter array and '' and other null values

PHP has a very complete set of functions for array operations, including filtering null values.

To filter all elements with empty values ​​in the array, you can directly use the array_filter() function. For example:

$entry = array(
             0 => 'foo',
             1 => false,
             2 => -1,
             3 => null,
             4 => '',
             5 => 0
          );
 
print_r(array_filter($entry));
 
 
array(
             0 => 'foo',
             1 => false,
             2 => -1,
             3 => null,
             4 => '',
             5 => 0
          );
print_r(array_filter($entry));

The above code will output:

Array
(
    [0] => foo
    [2] => -1
)
(
    [0] => foo
    [2] => -1
)

As you can see, the array_filter() function filters all elements whose value is false.

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:
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