How to delete empty elements in an array in php

PHPz
Release: 2023-04-18 09:40:27
Original
590 people have browsed it

In PHP programming, deleting empty elements in an array is a very common operation. Filtering out null values in the array can make the array data clearer and facilitate subsequent processing and use. This article will introduce readers to several methods of deleting empty elements in an array.

Use array_filter() function

array_filter() is a function in PHP built-in function for filtering arrays. This function uses a callback function to filter array elements and returns the filtered array. Using this function we can easily remove null values from the array.

The following is a sample code that uses the array_filter() function to filter null values:

$arr = array('apple', '', 'banana', null, 'cherry'); $res = array_filter($arr, function($val) { return !empty($val); }); print_r($res);
Copy after login

The output of the above sample code is:

Array ( [0] => apple [2] => banana [4] => cherry )
Copy after login
Copy after login
Copy after login

For the above sample code, first we An array $arr containing null values is created, and then the array_filter() function is used to filter the array. A Lambda function is used as a callback function. For each element in the array, it is checked whether the element is empty, and if it is not empty, Elements are added to the resulting array.

Use the unset() function to delete

Use the unset() function to delete null values in the array directly during the process of looping the array.

The following is a sample code that uses the unset() function to delete null values:

$arr = array('apple', '', 'banana', null, 'cherry'); foreach($arr as $key => $val) { if(empty($val)) { unset($arr[$key]); } } print_r($arr);
Copy after login

The output of the above sample code is:

Array ( [0] => apple [2] => banana [4] => cherry )
Copy after login
Copy after login
Copy after login

For the above sample code, we use The foreach loops the array $arr, and uses an if condition to determine whether each value in the array is empty. If so, use unset() to delete the element.

Use the array_diff() function

Using the array_diff() function you can also easily delete null values in the array.

The following is a sample code that uses the array_diff() function to delete null values:

$arr = array('apple', '', 'banana', null, 'cherry'); $filtered = array_diff($arr, array('', null)); print_r($filtered);
Copy after login

The output of the above sample code is:

Array ( [0] => apple [2] => banana [4] => cherry )
Copy after login
Copy after login
Copy after login

For the above sample code, we use array_diff () function to compare array $arr and array array('', null), and delete the intersection between the two arrays, that is, delete the null value.

Conclusion

This article introduces three methods in PHP to delete empty elements in an array. These methods can be selected and used according to the specific situation. No matter which method is used, deleting null values in the array can make the array data clearer and facilitate subsequent processing and use.

In actual development, we can choose to use different methods according to different needs. In terms of performance and effect, using the array_diff() function and array_filter() function is more efficient, while using the unset() function requires attention to modifying element key values to avoid unexpected results.

The above is the detailed content of How to delete 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
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!