Home > Backend Development > PHP Problem > How to remove empty elements from an array in php

How to remove empty elements from an array in php

PHPz
Release: 2023-04-20 14:25:03
Original
596 people have browsed it

In PHP programming, we often need to process arrays. During the process of processing arrays, we will encounter the need to remove empty elements in the array. Empty elements may include empty strings, NULL, false, etc. These elements may interfere with our processing and operations on arrays in some cases. This article will introduce how to use PHP functions to remove empty elements from an array.

1. Use the array function array_filter

PHP provides an array function array_filter, which can easily filter elements in the array. The function of the array_filter function is to filter out the elements in the array that are not NULL, false, empty string, etc., and return a new array composed of them.

The following is a usage example:

<?php
$arr = array("apple", false, "", null, "banana");
$result = array_filter($arr);
var_dump($result);
?>
Copy after login

The output result is:

array(2) {
  [0]=>
  string(5) "apple"
  [4]=>
  string(6) "banana"
}
Copy after login

As you can see, the $arr array in this example includes empty strings, false values ​​and NULL value. The new array $result obtained after filtering using the array_filter function only contains elements that are not NULL, false, or empty strings.

2. Custom filter function

If we need a more flexible filtering method, we can customize a filter function and then pass it to PHP's array_filter function for use.

The following is an example of a custom filter function:

<?php
/**
 * 过滤数组中的空字符串、null、false值
 * @param $value
 * @return bool
 */
function myFilter($value)
{
    if($value === "" || $value === null || $value === false)
    {
        return false;
    }
    return true;
}

$arr = array("apple", false, "", null, "banana");
$result = array_filter($arr, &#39;myFilter&#39;);
var_dump($result);
?>
Copy after login

Running this code will produce the same output as above. As you can see, here we define a filter function myFilter, whose function is to determine whether the given value is NULL, false or an empty string. If so, it returns false, otherwise it returns true. Then we pass this function as the second parameter to the array_filter function to complete the filtering.

Summary:

This article introduces two common methods of using PHP to remove empty elements in an array. One is to use PHP's array_filter function, and the other is a custom filter function. In actual programming, you can choose different methods according to specific needs in order to obtain a more flexible and efficient array processing method.

The above is the detailed content of How to remove empty elements from an array in php. For more information, please follow other related articles on the PHP Chinese website!

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