Summary of common PHP array functions

Source: Internet
Author: User
Tags random seed
: This article mainly introduces the summary of common PHP array functions (reprinted). For more information about PHP tutorials, see. I. basic functions of array operations
Key name and value of the array
Is_array ($ arr); // determines whether it is an array count ($ arr); // counts the number of groups in the array array_count_values ($ arr ); // count the number of times an element appears in the array. the returned result is an array array_values ($ arr). the array value array_keys ($ arr) is obtained. the array key name array_flip ($ arr) is obtained ); the values in the array are exchanged with the key names (if there are duplicates, the values will be overwritten) in_array ("apple", $ arr); retrieve applearray_search ("apple ", $ arr); search for apple in the array. if the return key name array_key_exists ("apple", $ arr) exists ); search whether the given key name exists in the isset ($ arr [apple]) array: Search whether the given key name exists in the array

Internal pointer of array
Current ($ arr); returns the current unit pos ($ arr) in the array; returns the current unit key ($ arr) in the array ); returns the key name prev ($ arr) of the current unit in the array; returns the internal pointer in the array to one next ($ arr ); move the internal pointer in the array forward an end ($ arr); point the internal pointer in the array to the last unit reset ($ arr; point the internal pointer in the array to the first unit each ($ arr); returns a constructed array of a key name/value of the current element of the array, and move the array pointer forward to a list ($ key, $ value) = each ($ arr) to obtain the key name and value of the current element of the array.

Conversion between arrays and variables
Extract ($ arr) is used to convert elements in the array into variables and import them to the current file. The key name is used as the variable name and the value is used as the variable value. note: (the second parameter is very important, see the manual) usage echo $ a; compact (var1, var2, var3); create an array with the given variable name

II. segmentation and filling of arrays
Array segmentation
Array_slice ($ arr,); extracts a segment from the array. this function ignores the key name array_splice ($ arr, array ("black", "maroon ")); A segment in the array can be taken out. Unlike the previous function, the returned sequence is deleted from the original array.

Split multiple arrays
Array_chunk ($ arr, 3, TRUE); you can divide an array into multiple values. TRUE indicates that the key name of the original array is retained.

Array filling
Array_pad ($ arr, 5, 'x'); fill an array to the specified length.

3. array and stack, first-in-first-out
Array_push ($ arr, "apple", "pear"); pushes one or more elements to the end of the array stack (inbound stack ), returns the number of elements in the input stack. array_pop ($ arr) pops up the last element in the array stack (output stack)

4. arrays and queues
Array_shift ($ arr); the first element in the array is removed and returned as a result (the length of the array is reduced by 1, the other elements are moved forward by one, and the number key name is changed to the zero technology, text key name unchanged) array_unshift ($ arr, "a", array (1, 2); insert one or more elements at the beginning of the array

V. callback functions
Array_walk ($ arr, 'function', 'Word'); use user functions to process each member in the array (the third parameter is passed to the callback function) array_map ("function ", $ arr1, $ arr2); can process multiple arrays (when two or more arrays are used, their length should be the same) array_filter ($ arr, "function "); use the callback function to filter every element in the array. if the callback function is TRUE, the current element of the array will be included in the returned result array, and the key name of the array will remain unchanged. array_reduce ($ arr, "function", "*"); convert to a single-value function (* is the first value of the array)

6. sorting arrays

Sort arrays by elements

Sort ($ arr); sort in ascending order (by which the second parameter is sorted). rsort ($ arr) ignores the array sorting of key names ); sort in ascending order (in which the second parameter is sorted). sort the usort ($ arr, "function") by the array with the key name is ignored "); sort the values in the array using the user-defined comparison function (there are two parameters in the function, 0 indicates equal, positive number indicates that the first parameter is greater than the second parameter, negative number indicates that the first parameter is less than the second parameter) ignore the array sorting asort ($ arr) of the key name; sort in ascending order (the second parameter is sorted in what way) sort the array of reserved key names ($ arr); sort by ascending order (by which the second parameter is sorted) sort by the array of reserved key names uasort ($ arr, "function"); use the user-defined comparison function to sort the values in the array (the function has two parameters, 0 indicates equal, positive number indicates that the first parameter is greater than the second parameter, negative number indicates that the first key is smaller than the second one.) sort the array with the reserved key name.


Sort arrays by key name
Ksort ($ arr); sort krsort ($ arr) in the forward order of key names; sort uksort ($ arr, "function") in reverse order of key names "); sort the key names in the array using the user-defined comparison function (there are two parameters in the function, 0 indicates equal, positive number indicates that the first parameter is greater than the second parameter, negative number indicates that the first parameter is less than the second parameter)

Sort by natural sorting
Natsort ($ arr); natural sorting (ignore key names) natcasesort ($ arr); natural sorting (ignore case sensitivity, ignore key names)

VII. array calculation
Sum of array elements
Array_sum ($ arr); calculates the sum of all elements in the array.

Array merging
Array_merge ($ arr1, $ arr2); merges two or more arrays (the same string key name, followed by the previous one, the same number key name, followed by no overwriting operation, instead, it is appended to the end.) "+" $ arr1 + $ arr2; only the last array_merge_recursive ($ arr1, $ arr2) is retained for the same key name; recursive merge operation, if the array contains the same string key name, these values are merged into an array. If a value is an array, it is merged into another array according to the corresponding key name. When the array has the same array key name, the latter value will not overwrite the original value, but will be appended

Array difference set
Array_diff ($ arr1, $ arr2); returns the result array array_diff_assoc ($ arr1, $ arr2, $ arr3) of the difference set. returns the result array of the difference set and compares the key names.

Array intersection
Array_intersect ($ arr1, $ arr2); returns the intersection result array array_intersect_assoc ($ arr1, $ arr2); returns the intersection result array, and compares the key names

8. other array functions
Range (); create an array array_unique ($ arr) that contains the specified range units; remove repeated values from the array, the original key name array_reverse ($ arr, TRUE) is retained in the new array. an array in the opposite order of the original array is returned, if the second parameter is TRUE, retain the original key name // srand (float) microtime () * 10000000); random seed trigger array_rand ($ arr, 2 ); randomly retrieve one or more implode ($ glue, $ arr) elements from the array; // concatenate a string into a shuffle ($ arr) separated by $ glue ); disrupt the order of arrays

The above section describes the summary of common PHP array functions (reprinted), including some content, and hopes to help those who are interested in PHP tutorials.

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.