Home  >  Article  >  Backend Development  >  PHP filters empty values ​​​​such as 0, null, false and '' in the array

PHP filters empty values ​​​​such as 0, null, false and '' in the array

angryTom
angryTomforward
2019-10-15 17:29:273080browse

PHP filters empty values ​​​​such as 0, null, false and '' in the array

#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));

The above code will output:

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

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

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of PHP filters empty values ​​​​such as 0, null, false and '' in the array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:www.awaimai.com. If there is any infringement, please contact admin@php.cn delete