PHP introduces how to delete empty array values

Source: Internet
Author: User

To put it bluntly, the null values removed from the array are strongly written into foreach or while. These two syntax structures are used to delete the empty elements in the array. The simple code is as follows:
Copy codeThe Code is as follows:
<? Php
Foreach ($ arr as $ k =>$ v ){
If (! $ V)
Unset ($ arr [$ k]);
}
?>

In addition, the self-feeling is quite good, but this efficiency is not very high. I have tried to convert $ arr into an object first, and then delete it using the characteristics of the object, because: foreach is to copy the array of the current operation. For each operation, foreach copies a variable. If there are too many foreach entries in the page, it will be a huge consumption.

When hanging out on the Internet, I was surprised to see a prompt using array_filter. I opened the manual and looked at it. I found that I had been keeping a guard of Baoshan, but I didn't know how to use it.

The array_filter function uses the callback function to filter the array. It is always thought that the callback function can be used for processing, but it is not found that there is another sentence in the manual. If there is no callback function, by default, items whose values are false are deleted.
Copy codeThe Code is as follows:
<? Php

$ Entry = array (
0 => 'foo ',
1 => false,
2 =>-1,
3 => null,
4 =>''
);

Print_r (array_filter ($ entry ));
?>

The output value is:
Copy codeThe Code is as follows:
Array
(
[0] => foo
[2] =>-1
)

In the future, I still need to read more manuals ...... Like array_slice, it is also a good thing. Unfortunately, I have never paid attention to it before.

Appendix: Another example

Copy codeThe Code is as follows: $ strDelCodes = "A; B; C; D ";
$ RsArray = array_values (array_unique (array_diff (split (";", $ strDelCodes), array (""))));

The value saved in the array $ rsArray is a B C D.

The array_values () function returns an array containing all the key values in the given array without retaining the key name.

The array_diff () function returns the number of difference sets of two arrays. This array includes all the key values in the compared array, but not in any other parameter array.

The array_unique () function removes repeated values from the array and returns the result array. When the values of several array elements are equal, only the first element is retained, and other elements are deleted.

The Input key of the returned array remains unchanged.

The array_merge () function combines two or more numbers into an array.

If the key name already exists, the key value of the key is the value corresponding to the last key name (the subsequent one overwrites the previous one ). If the array is a digital index, the key name is re-indexed continuously.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.