Home >Backend Development >PHP Tutorial >Introduction to how to delete null values in an array in PHP_PHP Tutorial
Ashamed to say, in the past, when removing empty values from an array, foreach or while were forced. Use these two syntax structures to delete empty elements in the array. The simple code is as follows:
And I feel pretty good about it, but it’s not very efficient. I’ve tried it before, first converting $arr to an object, and then using the characteristics of the object to delete it, because: foreach copies the currently operated array. Every time you operate foreach, a variable is copied. If there are too many foreach on the page, it will be a big waste.
When I was wandering around on the Internet, I was surprised to see people suggesting using array_filter. I opened the manual and took a look, and found that I had been guarding a treasure mountain but didn't know how to use it.
The function of the array_filter function is to use the callback function to filter the array. I always thought that the callback function can be used to process it, but I didn’t find that there is another sentence in the manual. If there is no callback function, then the default is to delete the array whose value is false. project.
Copy code
The code is as follows:$strDelCodes = "A;B;;C;;C;D;;;D;D ";
$rsArray = array_values (array_unique (array_diff (split (";", $strDelCodes), array (""))));The array_merge() function merges two or more arrays into one array.
If there are duplicate key names, the key value of the key will be the value corresponding to the last key name (the later one will overwrite the previous one). If the array is numerically indexed, the key names are re-indexed consecutively.
http://www.bkjia.com/PHPjc/754340.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/754340.html