How to filter empty elements in an array in php

PHPz
Release: 2023-04-26 09:46:30
Original
618 people have browsed it

With the continuous development of web development, server-side programming languages ​​are gradually becoming more colorful. PHP can be said to be one of the most commonly used languages. As a high-level scripting language on the server side, PHP is very powerful and easy to use. In actual development, we often need to store data through arrays. However, sometimes empty elements appear in the array, which will affect our data processing and output. Therefore, this article will introduce in detail how to filter empty elements in PHP arrays.

  1. Use the array_filter() function to filter empty elements in the array

array_filter() is a built-in function in PHP, which can filter the elements in an array and Returns a new array containing only elements that meet the condition. We can use the array_filter() function to filter out empty elements in the array. The sample code is as follows:

$array = array("apple", "orange", "", "banana", "", "grape");
//使用array_filter()函数过滤数组中的空元素
$new_array = array_filter($array);
//输出过滤后的数组
print_r($new_array);
Copy after login

Run the above code and the output result is as follows:

Array
(
    [0] => apple
    [1] => orange
    [3] => banana
    [5] => grape
)
Copy after login

As you can see, the array_filter() function is used to filter out the empty elements in the array and return only A new array containing non-empty elements.

  1. Use a foreach loop to traverse the array and filter empty elements

In addition to using the array_filter() function, we can also use a foreach loop to traverse the array and use the if statement to determine whether the element is is empty, thereby filtering out empty elements in the array. The sample code is as follows:

$array = array("apple", "orange", "", "banana", "", "grape");
//遍历数组并过滤空元素
foreach($array as $key => $value){
    if(empty($value)){
        unset($array[$key]);
    }
}
//重新排列数组的键
$array = array_values($array);
//输出过滤后的数组
print_r($array);
Copy after login

Run the above code, and the output result is as follows:

Array
(
    [0] => apple
    [1] => orange
    [2] => banana
    [3] => grape
)
Copy after login

As you can see, this method can also filter out empty elements in the array.

It should be noted that using the unset() function to delete array elements will affect the index order of the array, so you need to use the array_values() function to rearrange the keys of the array to get the correct filtering result.

  1. Use the array_map() function to filter empty elements in the array

In addition to using array_filter() and foreach loops, we can also use the array_map() function to filter the array empty element. The function of the array_map() function is to apply a callback function to each element of one or more arrays and return a new array. The sample code is as follows:

$array = array("apple", "orange", "", "banana", "", "grape");
//过滤数组中的空元素
$new_array = array_map(function($value){
    if(empty($value)){
        return false;
    }else{
        return $value;
    }
}, $array);
//去除新数组中的false元素
$new_array = array_filter($new_array);
//重新排列数组的键
$new_array = array_values($new_array);
//输出过滤后的数组
print_r($new_array);
Copy after login

When you run the above code, the output result is also to filter out the empty elements in the array and get a new array containing only non-empty elements.

  1. Conclusion

This article introduces three methods of filtering empty elements in PHP arrays: array_filter(), foreach loop and array_map(). As we all know, arrays are one of the most commonly used data types in PHP, and filtering arrays is an inevitable task in daily development. The methods provided in this article can play a very good role in actual development, helping developers process array data more easily and improve development efficiency.

The above is the detailed content of How to filter empty elements in 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!