Home >Backend Development >PHP Tutorial >PHP implements method to remove empty or certain value elements from an array

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

高洛峰
高洛峰Original
2017-02-10 16:52:382050browse

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);

Return result:

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

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!

Statement:
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