How to clear empty values ​​in array in php

王林
Release: 2023-03-04 12:52:02
Original
4026 people have browsed it

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

Code implementation:

<?php   
$entry = array(   
             0 => &#39;foo&#39;,   
             1 => false,   
             2 => -1,   
             3 => null,   
             4 => &#39;&#39;  
          );   
 
print_r(array_filter($entry));   
?>
Copy after login

Output result:

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

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!

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