How to remove empty elements from an array in php

PHPz
Release: 2023-04-04 14:10:02
Original
633 people have browsed it

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

Running results:

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

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

Running results:

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

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

Running results:

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

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

Running results:

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!