Home > Article > Backend Development > PHP method to remove empty or certain value elements from an array
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 in need can refer to the following
Examples 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: The array_filter
array_filter() function uses a 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 form the results into a new array. If the original array is an associative array, the key names remain unchanged.
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP implements the method of outputting the arrangement or combination of multiple elements
php implements the full arrangement of the array, elementsmethods of all combinations
php file_get_contents implements obtaining the array in the file Methods of element
The above is the detailed content of PHP method to remove empty or certain value elements from an array. For more information, please follow other related articles on the PHP Chinese website!