Home >Backend Development >PHP Problem >How to remove null values from php array
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.
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!