Home  >  Article  >  Backend Development  >  How to delete null values ​​in an array in php

How to delete null values ​​in an array in php

王林
王林Original
2020-08-06 13:26:543065browse

php method to delete null values ​​​​in an array: You can use the array_filter() function to achieve this. The array_filter() function uses a callback function to filter elements in the array. The specific usage method is as follows: [array_filter($array)].

How to delete null values ​​in an array in php

#You can use the array_filter() function to delete null values ​​​​in the array.

(Recommended tutorial: php graphic tutorial)

array_filter() function uses a callback function to filter the elements in the array. 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, and the array key name remains unchanged.

Syntax:

array array_filter(array $array [, callable $callback[, int $flag = 0]])

Parameters:

  • ##array Required. Specifies the array to filter.

  • callback Optional. Specifies the callback function to be used.

  • #flag Optional. Determines the form of parameters received by callback.

(Learning video recommendation:

Introduction to Programming)

Code implementation:

 "abc", 'b' => "bcd",'c' =>"cde",'d' =>"def",'e'=>"");
    $b= array_filter($array);

    print_r($b);
?>

Output result:

Array (
[a] => abc
[b] => bcd
[c] => cde
[d] => def
)

The above is the detailed content of How to delete null values ​​in an 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