PHP implements method to remove empty or certain value elements from an array

高洛峰
Release: 2023-03-05 17:16:01
Original
1984 people have browsed it

This article mainly introduces the method of PHP to remove empty or certain value elements in the array, involving the related operation skills of PHP using array_filter to filter the array. Friends who need it can refer to the example of this article

Describes how PHP implements the method of removing empty or certain value elements from an array. Share it with everyone for your reference, the details are as follows:

Two functions array_filter and create_function are used to remove empty elements or elements with a certain value in the array

Look first An example:

$array = Array ( [0] => 1 ,[1] => 2, [2] => 3, [3] => 4,[4] => '',[5] => '' );
$array = array_filter($array,create_function('$v','return !empty($v);'));
print_r($array);
Copy after login

Return result:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Copy after login

Function introduction: array_filter

array_filter() function uses the callback function to filter the elements in the array. If the custom filter function returns true, the current value of the operated array will be included in the returned result array, and the result will be formed into a new array. If the original array is an associative array, the key names remain unchanged.

For more PHP methods to remove empty or certain value elements from an array, please pay attention to the PHP Chinese website!

Related labels:
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!