How to remove null values ​​from php array

藏色散人
Release: 2023-03-06 21:34:01
Original
6928 people have browsed it

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));
Copy after login

The above code will output:

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

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!

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