Home  >  Article  >  Backend Development  >  How to clear empty values ​​in array in php

How to clear empty values ​​in array in php

王林
王林Original
2020-08-13 15:34:253868browse

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.

How to clear empty values ​​in array in php

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:

 '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!

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