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

How to remove empty elements from an array in php

PHPz
PHPzOriginal
2023-04-04 10:39:38600browse

PHP is a popular server-side programming language commonly used for web development and the creation of dynamic websites. In PHP programming, array is a common data type. Sometimes we need to filter out empty elements in an array. This article will introduce you how to remove empty elements from an array.

1. Use foreach loop to traverse the array

You can use foreach loop to traverse the array, and then use the empty() function to determine whether each element is empty. If it is empty, use the unset() function to move it. Remove this element. The code is as follows:

$arr = array('php', '', 'java', '', 'python');
foreach($arr as $key=>$value){
    if(empty($value)){
        unset($arr[$key]);
    }
}
print_r($arr);

Running results:

Array
(
    [0] => php
    [2] => java
    [4] => python
)

2. Use the array_filter() function

PHP provides a built-in function array_filter(), which can filter out the elements in the array Empty element. The code is as follows:

$arr = array('php', '', 'java', '', 'python');
$arr = array_filter($arr);
print_r($arr);

Running results:

Array
(
    [0] => php
    [2] => java
    [4] => python
)

3. Use the array_diff() function

In addition, you can also use the array_diff() function, which can return one of two arrays We can perform a difference operation on an empty array and the original array and return the difference result. The code is as follows:

$arr = array('php', '', 'java', '', 'python');
$arr = array_diff($arr, array(''));
print_r($arr);

Running results:

Array
(
    [0] => php
    [2] => java
    [4] => python
)

4. Use the array_reduce() function

Finally, we can use the array_reduce() function, which iterates from left to right Array, combining each element with the previous result. We can determine whether the element is empty in the callback function. If it is empty, return the previous result, otherwise add the element to the result array. The code is as follows:

$arr = array('php', '', 'java', '', 'python');
$arr = array_reduce($arr, function($result, $val){
    if(!empty($val)){
        $result[] = $val;
    }
    return $result;
}, array());
print_r($arr);

Running results:

Array
(
    [0] => php
    [1] => java
    [2] => python
)

The above are several methods for removing empty arrays. You can choose the method that suits you according to your needs.

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!

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