Common PHP array functions-php Tutorial

Source: Internet
Author: User
Tags random seed
Common PHP array functions I. basic functions of array operations

Key name and value of the array

Array_values ($ arr); obtain the array value array_keys ($ arr); obtain the array key name array_flip ($ arr ); values in the array are exchanged with key names (if there is a duplicate, the previous one will be overwritten) in_array ("apple", $ arr); search for apple array_search ("apple ", $ arr); search for apple in the array. if the return key name array_key_exists ("apple", $ arr) exists, search for whether the given key name exists in the array.

Isset ($ arr [apple]): retrieves whether a 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); direct the internal pointer in the array to the last reset ($ arr) unit ); 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 moves the array pointer one byte forward.

List ($ key, $ value) = each ($ arr); 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. you can refer to the manual for use) echo $;

Compact (var1, var2, var3); create an array with the given variable name

II. segmentation and filling of arrays

Array segmentation

Array_slice ($ arr,); removes a segment from the array. this function ignores the key name.

Array_splice ($ arr, array ("black", "maroon"); removes a segment of the array. 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

Array_push ($ arr, "apple", "pear"); pushes one or more elements to the end of the array stack (inbound stack) and returns the number of inbound stack elements

Array_pop ($ arr); pops up the last element of the array stack (Out 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 each element in the array. if the callback function is TRUE, the current element of the array is included in the returned result array, and the key name of the array remains unchanged.

Array_reduce ($ arr, "function", "*"); convert to a single-value function (* as the first value of the array)

6. sorting arrays

Sort arrays by element values

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 the reserved key names ($ arr); sort the key names in ascending order (the second parameter is sorted in what way)

Uasort ($ arr, "function"); sort the values in the array using the user-defined comparison function (the function has two parameters, 0 indicates equal, positive number indicates that the first key is greater than the second, negative number indicates that the first key is smaller than the second .)

Sort arrays by key name

Ksort ($ arr); sort krsort ($ arr) in the forward order of key names; sort by key name in reverse order

Uksort ($ arr, "function"); sort the key names in the array using the user-defined comparison function (the function has two parameters, 0 indicates equal, A positive number indicates that the first is greater than the second, and a negative number indicates that the first is smaller than the second)

Sort by natural sorting

Natsort ($ arr); natural sorting (ignore key names)

Natcasesort ($ arr); natural sorting (case insensitive, key names ignored)

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 back) "+" $ arr1 + $ arr2; keep only the first key name for the same (for details, refer to: http://www.qianyunlai.com/blog/138.html)

Array_merge_recursive ($ arr1, $ arr2); 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 of the difference set.

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

Array intersection

Array_intersect ($ arr1, $ arr2); returns an array of intersection results

Array_intersect_assoc ($ arr1, $ arr2); returns an array of intersection results 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 elements from the array shuffle ($ arr); disrupt the order of the array

I. basic functions of array operations

Key name and value of the array

Array_values ($ arr); obtain the array value array_keys ($ arr); obtain the array key name array_flip ($ arr ); values in the array are exchanged with key names (if there is a duplicate, the previous one will be overwritten) in_array ("apple", $ arr); search for apple array_search ("apple ", $ arr); search for apple in the array. if the return key name array_key_exists ("apple", $ arr) exists, search for whether the given key name exists in the array.

Isset ($ arr [apple]): retrieves whether a 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); direct the internal pointer in the array to the last reset ($ arr) unit ); 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 moves the array pointer one byte forward.

List ($ key, $ value) = each ($ arr); 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. you can refer to the manual for use) echo $;

Compact (var1, var2, var3); create an array with the given variable name

II. segmentation and filling of arrays

Array segmentation

Array_slice ($ arr,); removes a segment from the array. this function ignores the key name.

Array_splice ($ arr, array ("black", "maroon"); removes a segment of the array. 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

Array_push ($ arr, "apple", "pear"); pushes one or more elements to the end of the array stack (inbound stack) and returns the number of inbound stack elements

Array_pop ($ arr); pops up the last element of the array stack (Out 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 each element in the array. if the callback function is TRUE, the current element of the array is included in the returned result array, and the key name of the array remains unchanged.

Array_reduce ($ arr, "function", "*"); convert to a single-value function (* as the first value of the array)

6. sorting arrays

Sort arrays by element values

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 the reserved key names ($ arr); sort the key names in ascending order (the second parameter is sorted in what way)

Uasort ($ arr, "function"); sort the values in the array using the user-defined comparison function (the function has two parameters, 0 indicates equal, positive number indicates that the first key is greater than the second, negative number indicates that the first key is smaller than the second .)

Sort arrays by key name

Ksort ($ arr); sort krsort ($ arr) in the forward order of key names; sort by key name in reverse order

Uksort ($ arr, "function"); sort the key names in the array using the user-defined comparison function (the function has two parameters, 0 indicates equal, A positive number indicates that the first is greater than the second, and a negative number indicates that the first is smaller than the second)

Sort by natural sorting

Natsort ($ arr); natural sorting (ignore key names)

Natcasesort ($ arr); natural sorting (case insensitive, key names ignored)

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 back) "+" $ arr1 + $ arr2; keep only the first key name for the same (for details, refer to: http://www.qianyunlai.com/blog/138.html)

Array_merge_recursive ($ arr1, $ arr2); 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 of the difference set.

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

Array intersection

Array_intersect ($ arr1, $ arr2); returns an array of intersection results

Array_intersect_assoc ($ arr1, $ arr2); returns an array of intersection results 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 elements from the array shuffle ($ arr); disrupt the order of the array

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.