- foreach ($arr as $k = = $v) {
- if (! $v)
- Unset ($arr [$k]);
- }
- ?>
Copy CodeAnd 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 PHP manual below a sentence, if there is no callback function, then the default is to delete the value of the array of false items.
$entry = Array (
- 0 = ' Foo ',
- 1 = False,
- 2 =-1,
- 3 = NULL,
- 4 = "
- );
Print_r (Array_filter ($entry));
- ?>
Copy CodeOutput value: Array ([0] = foo [2] +-1) It seems that you should read more manuals later ... Just like Array_slice is a good thing. But I never noticed it before. Attached: Another example
- $strDelCodes = "A; B;; C;; c;d;;D; D ";
- $rsArray = Array_values (Array_unique (Split (";", $strDelCodes), Array (""))));
Copy CodeThe value stored in the array $rsarray is: a B C darray_values () function returns an array containing all the key values in the given array, but does not retain the key name. 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. |