Introduction to _php Example of PHP's method of deleting empty array values

Source: Internet
Author: User
Ashamed to say, before the null value of the array is either strong write foreach or while, using these two syntax structure to delete empty elements in the array, the simple code is as follows:
Copy CodeThe code is as follows:
foreach ($arr as $k = = $v) {
if (! $v)
Unset ($arr [$k]);
}
?>

And self-feeling is pretty good, but this is not high efficiency also once tried, first $arr to object, and then use the object's characteristics to delete, because: foreach is the current operation of the array copy, each action of foreach, is a copy of a variable, If there are too many foreach in the page, it will be a big drain.

When I was wandering around the internet, I was surprised to see people with Array_filter. Open the manual and look at it and find that you have been guarding a baoshan but don't know how to use it.

The function of the Array_filter function is to use the callback function to filter the array, always thought that the callback function can be processed, but did not find the manual below a sentence, if there is no callback function, then the default is to delete the value of the array of false items.
Copy the Code code as follows:

$entry = Array (
0 = ' Foo ',
1 = False,
2 =-1,
3 = NULL,
4 = "
);

Print_r (Array_filter ($entry));
?>

The output values are:
Copy the Code code as follows:
Array
(
[0] = foo
[2] =-1
)

It seems that the future is to read more manuals ah ... Just like Array_slice is a good thing. But I never noticed it before.

Attached: Another example

Copy the Code code as follows: $strDelCodes = "A; B;; C;; c;d;;D; D ";
$rsArray = Array_values (Array_unique (Split (";", $strDelCodes), Array (""))));

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

The Array_values () function returns an array that contains all the key values in the given array, but does not preserve the key names.

The Array_diff () function returns an array of difference sets for two arrays. The array includes all of the key values in the array being compared, but not in any other parameter array.

The Array_unique () function shifts the duplicate values in the array and returns the result arrays. When the values of several array elements are equal, only the first element is preserved, and the other elements are deleted.

The key name in the returned array is not changed.

The Array_merge () function merges two or more arrays into an array.

If the key name is duplicated, the key's key value is the value corresponding to the last key name (followed by overwriting the previous one). If the array is a numeric index, the key name is re-indexed in a sequential manner.

  • 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.