Home >Backend Development >PHP Tutorial >How to remove null values ​​or empty elements from an array in PHP

How to remove null values ​​or empty elements from an array in PHP

php中世界最好的语言
php中世界最好的语言Original
2018-04-11 11:25:463171browse

This time I will show you how to remove null values ​​or empty elements in an array with PHP. What are the precautions for removing null values ​​or empty elements from an array with PHP? , the following is a practical case, let’s take a look. Two

functions

array_filter、create_function are used to remove empty elements or elements with a certain value in the array. Let’s look at an example first:

# The code is as follows$array= Array ( [0] => 1 ,[1] => 2, [2] => 3, [3] => 4,[ 4] =>'',[5] =>''); Return result:

$array=array_filter($array,create_function('$v','

return

!empty( $v);'));print_r($array);

code show as belowArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) Function introduction: array_filter

array_filter() function uses

callback function

to filter the elements in the array. If the custom filter function returns true, the current value of the array being operated on will be included in the returned result array, and the results will be formed into a new array. If the original array is an associative array, the key names remain unchanged. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed steps of creating session method in php


php implements a log function


The above is the detailed content of How to remove null values ​​or empty elements from an array in PHP. For more information, please follow other related articles on 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