For a one-dimensional PHP array, how do I erase an element whose value is empty? The direct method is a foreach loop, one judgment to exclude. But this method is slightly more complex, the following share a way to see today, very concise is the first time to see this writing, record.
Suppose that there is one one-dimensional array as follows:
$array =array (0=> ' Alixixi ',1=> ',2=> ' com ',3=> ');
Clearing the empty elements in the array can be written like this:
$array =array_filter ($array, create_function (' $v ', ' return!empty ($v); ");
Print_r ($array);
Output result: Array ([0] => Alixixi [2] => com);
Briefly analyze the two more important functions in the above code:
The array array_filter (array array,string function) function filters the elements in array arrays using callback function functions, and if the custom filter functions function returns True, The current value of the array being manipulated is included in the returned array of results, and the result is formed into a new array. If the original array is an associative array, the key name remains unchanged.
String Create_function (String $args, String $code) creates an anonymous function.