php remove empty elements from array

王林
Release: 2023-05-06 20:30:09
Original
487 people have browsed it

PHP is a popular programming language, especially widely used in web development. During the development process, array data is often processed, and sometimes there may be empty elements in the array. In this case, these empty elements need to be deleted. This article will introduce how to remove empty elements from an array in PHP.

1. Use the array_filter function

The array_filter function can be used to filter elements in an array and delete elements that do not meet the conditions from the array. This function needs to pass in an array and a callback function. The callback function judges each element. If it returns true, the element is retained, and if it returns false, the element is deleted. Therefore, you can determine whether it is a non-empty element in the callback function.

The following is an example code that uses the array_filter function to remove empty elements from an array:

$array = array("a", "b", "", "c", null, "d");
$result = array_filter($array, function($value){
    return $value !== "" && $value !== null;
});
print_r($result);
Copy after login

In this code, $array is the array to be processed, and the array_filter function is used to filter it and keep non-empty elements. element. The judgment condition in the callback function is $value !== "" && $value !== null, that is, elements whose values ​​are empty strings and null are excluded. The final result will output the array after removing empty elements: Array ( [0] => a [1] => b [3] => c [5] => d ).

2. Use foreach loop

Different from the array_filter function, using foreach loop requires traversing the entire array, judging whether the elements are empty one by one, and adding non-empty elements to a new array. The following is an example code that uses a foreach loop to remove empty elements from an array:

$array = array("a", "b", "", "c", null, "d");
$result = array();
foreach($array as $value){
    if($value !== "" && $value !== null){
        $result[] = $value;
    }
}
print_r($result);
Copy after login

In this code, a foreach loop is used to traverse the original array, and each element is judged whether it is empty. Non-empty elements are added to the $result array. middle. The final result will output the array after removing empty elements: Array ( [0] => a [1] => b [3] => c [5] => d ).

3. Use the array_diff function

If there are only empty elements in the array, you can directly use the array_diff function to remove them. This function can be used to compare multiple arrays and return values ​​that appear in the first array but not in all the remaining arrays, so just compare the original array with a new array containing only empty elements using array_diff.

The following is an example code that uses the array_diff function to remove empty elements from an array:

$array = array("a", "b", "", "c", null, "d");
$empty_array = array("");
$result = array_diff($array, $empty_array);
print_r($result);
Copy after login

In this code, the original array $array and a new array $empty_array containing only empty elements are compared using array_diff. Keep the elements that are not in $empty_array, and the final result will output the array after removing the empty elements: Array ( [0] => a [1] => b [3] => c [5] => d ).

Summary:

This article introduces three methods of removing empty elements from an array in PHP: using the array_filter function, using the foreach loop and using the array_diff function. These methods can be used to handle empty elements in an array and return a new array that does not contain empty elements. In actual development, the appropriate method should be selected based on needs.

The above is the detailed content of php remove empty elements from array. 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!