php method to clear empty values in an array: You can use the array_filter() function to clear empty values in an array. This function will pass each key value in the input array to the callback function. If there is no callback function, the elements with a value of false in the array will be deleted by default.
array_filter() function uses a callback function to filter the elements in the array.
(Recommended tutorial: php graphic tutorial)
This function passes each key value in the input array to the callback function. If the callback function returns true, the current key value in the input array is returned to the result array. Array key names remain unchanged.
Note: If there is no callback function, the default is to delete items with a false value in the array.
(Video tutorial recommendation: php video tutorial)
Syntax:
array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )
Code implementation:
<?php $entry = array( 0 => 'foo', 1 => false, 2 => -1, 3 => null, 4 => '' ); print_r(array_filter($entry)); ?>
Output result:
Array( [0] => foo [2] => -1 )
The above is the detailed content of How to clear empty values in array in php. For more information, please follow other related articles on the PHP Chinese website!