菜单

The method of removing empty or value element in array by PHP

2017年11月22日 - PHPer

In this paper, an example is given to illustrate how PHP implements the removal of null or value elements in an array. To share with you for your reference, as follows:

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

Look at an example first:

 

Code is as follows

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

Code is as follows

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

Function introduction: array_filter

The array_filter () function filters the elements in the array with the callback function. If the custom filter function returns true, the current value of the array being manipulated will be included in the returned result array, and the result will be a new array. If the original array is an associative array keys are preserved.

发表评论

电子邮件地址不会被公开。