Home > Backend Development > PHP Tutorial > Detailed explanation of how to delete null value instances in an array in PHP

Detailed explanation of how to delete null value instances in an array in PHP

伊谢尔伦
Release: 2023-03-11 09:44:01
Original
1264 people have browsed it

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:

<?php   
foreach( $arr as $k=>$v){   
    if( !$v )   
        unset( $arr[$k] );   
}   
?>
Copy after login

And I feel pretty good about myself, 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 current array of operations, and every operation Each foreach copy copies a variable. If there are too many foreachs on the page, it will be a huge waste.

When I was wandering around on the Internet, I was surprised to see someone 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 it can only be processed by the callback function, but I didn’t find that there is a sentence below in the manual. If there is no callback function, then The default is to delete items with a false value in the array.

<?php   
$entry = array(   
             0 => &#39;foo&#39;,   
             1 => false,   
             2 => -1,   
             3 => null,   
             4 => &#39;&#39;  
          );   
print_r(array_filter($entry));   
?>
Copy after login

The output value is:

Array   
(   
    [0] => foo   
    [2] => -1   
)
Copy after login

It seems that you still need to read more manuals in the future... Just like array_slice, it is also a good thing. Too bad I never noticed it before.

Attachment: Another example

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 (""))));
Copy after login

The value stored in the array $rsArray is: A B C D

array_values() function returns an array containing all key values ​​in the given array, but does not retain the key names.

array_diff() function returns the difference array of two arrays. This array contains all keys that are in the array being compared, but are not in any of the other parameter arrays.

array_unique() function removes duplicate values ​​from an array and returns the resulting array. When the values ​​of several array elements are equal, only the first element is retained and the other elements are deleted.

The key names in the returned array remain unchanged.

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.

The above is the detailed content of Detailed explanation of how to delete null value instances in an array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template