How to remove null values ​​​​from an array in php

王林
Release: 2023-02-28 20:56:01
Original
3674 people have browsed it

How to remove null values ​​​​from an array in php

To remove null values ​​​​in the array, you can consider using the array_filter() function.

Function introduction:

array_filter() function uses a callback function to filter 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. Array key names remain unchanged.

Grammar format:

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

Code example:

<?php
    $array = array(&#39;a&#39; => "abc", &#39;b&#39; => "bcd",&#39;c&#39; =>"cde",&#39;d&#39; =>"def",&#39;e&#39;=>"");
    $b= array_filter($array);

    print_r($b);
?>
Copy after login

Output result:

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

For more related tutorials, please visit php中文网.

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