PHP Common Array functions

Source: Internet
Author: User
Tags array length compact pear random seed rewind shuffle
basic functions of array manipulation

The key name and value of the array

Array_values ($arr); Gets the value of the array Array_keys ($arr); Gets the key name of the array array_flip ($arr); The values in the array are exchanged with the key names (if there is a repetition of the previous one will be overwritten by the following) In_array ("Apple", $arr); Retrieve Apple Array_search ("Apple", $arr) in the array; Retrieve Apple in the array if there is a return key name Array_key_exists ("Apple", $arr); Retrieves whether the given key name exists in the array

Isset ($arr [Apple]): Retrieves whether the given key name exists in the array

An internal pointer to an array

Current ($arr); Returns the current cell POS ($arr) in the array; Returns the current cell key ($arr) in the array; Returns the key name of the current cell in the array prev ($arr); Rewind the internal pointer in the array back to a next ($arr); Moves the inner pointer in the array forward one end ($arr); Point the inner pointer in the array to the last unit reset ($arr); Points the inner pointer in the array to the first cell each ($arr); Constructs an array that returns a key name/value of the current element of the array and moves the array pointer forward one

List ($key, $value) =each ($arr); Gets the key name and value of the current element of the array

Conversions between arrays and variables

Extract ($arr); used to convert the elements in an array into variables into the current file, the key name as the variable name, and the value as the variable value. Note: (The second parameter is very important, can see the manual use) method of use echo $a;

Compact (VAR1,VAR2,VAR3); Create an array with the given variable name

Ii. segmentation and padding of arrays

Fragment of an array

Array_slice ($arr, 0, 3); You can take a paragraph out of the array, and this function ignores the key name

Array_splice ($arr, 0,3,array ("Black", "maroon")); You can take a paragraph out of an array, unlike the previous function, in which the returned sequence is removed from the original array.

Splitting multiple arrays

Array_chunk ($arr, 3,true); You can split an array into multiple, true to preserve the key name of the original array

Padding of arrays

Array_pad ($arr, 5, ' X '); Fills an array into a set length

Three, arrays and stacks

Array_push ($arr, "apple", "pear"); Presses one or more elements into the end of the array stack (into the stack), returning the number of elements in the stack

Array_pop ($arr); POPs the last element of the array stack (out of the stack)

Iv. Arrays and queues

Array_shift ($arr); The first element in the array is moved out and returned as a result (array length minus 1, other elements move forward one bit, number key name changed from zero technology, text key name unchanged)

Array_unshift ($arr, "a", array); inserts one or more elements at the beginning of the array

Five, callback function

Array_walk ($arr, ' function ', ' words '); Use the user function to process each member in the array (the third argument is passed to the callback function) Array_map ("function", $arr 1, $arr 2); Multiple arrays can be processed (when two or more arrays are used, their lengths should be the same) Array_filter ($arr, "function"); Using a 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-valued function (* The first value of an array)

Vi. Ordering of arrays

Sort by array of element values

Sort ($arr); Sort by order of small to large (the second parameter is sorted by what sort) and the array ordering Rsort ($arr) that ignores the key name; Sort by order of large to small (the second parameter is sorted by what sort) and the array ordering Usort ($arr, "function") that ignores the key name; Use a user-defined comparison function to sort the values in the array (there are two parameters in function, 0 for equality, positive for the first one greater than the second, negative for the first less than the second) to sort Asort ($arr) for arrays that ignore key names; Sort by order of small to large (the second parameter is sorted by what sort) and the array of reserved key names Arsort ($arr); Sort by order of large to small (the second parameter is sorted by what sort) the array of reserved key names

Uasort ($arr, "function"); Use a user-defined comparison function to sort the values in the array (function with two parameters, 0 for equality, positive for the first one greater than the second, negative for the first one less than the second) to sort the arrays of reserved key names

Sort arrays by key names

Ksort ($arr); Sort Krsort ($arr) in positive order by key name; Sort by key name in reverse order

Uksort ($arr, "function"); Use a user-defined comparison function to sort the key names in the array (there are two parameters in function, 0 for equality, positive for the first greater than the second, and negative for the first less than the second one)

Sort by natural sort

Natsort ($arr); Natural sort (ignores key names)

Natcasesort ($arr); Natural sort (ignores case, ignores key name)

Vii. Calculation of arrays

Sum of array elements

Array_sum ($arr); Sums all elements inside an array

Merging of arrays

Array_merge ($arr 1, $arr 2); Merge two or more arrays (the same string key name, followed by overwriting the previous, same numeric key name, followed by no overwrite operation, but appended to the back) "+" $arr 1+ $arr 2; Keep only the first one for the same key name (for more information, refer to: http://www.qianyunlai.com/blog/138.html)

Array_merge_recursive ($arr 1, $arr 2); Recursive merge operations, if the array has the same string key name, these values will be merged into an array. If a value is itself an array, it will be 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 append to the back

The difference set of an array

Array_diff ($arr 1, $arr 2); Returns an array of difference results

ARRAY_DIFF_ASSOC ($arr 1, $arr 2, $arr 3); Returns the array of difference results, and the key names are also compared

Intersection of arrays

Array_intersect ($arr 1, $arr 2); Returns an array of intersection results

ARRAY_INTERSECT_ASSOC ($arr 1, $arr 2); Returns an array of intersection results, with key names also being compared

Eight, other array functions

Range (0,12); Creates an array containing the specified range of cells Array_unique ($arr); The duplicate values in the array are removed, and the original key names Array_reverse ($arr, TRUE) are preserved in the new arrays; Returns an array in which the cell order is opposite the original array, and if the second argument is true, preserve the original key name//srand ((float) microtime () *10000000); Random seed Trigger Array_rand ($arr, 2); Randomly extracts one or more elements from an array shuffle ($arr); Disturb the order of the arrays

Basic functions of array manipulation

The key name and value of the array

Array_values ($arr); Gets the value of the array Array_keys ($arr); Gets the key name of the array array_flip ($arr); The values in the array are exchanged with the key names (if there is a repetition of the previous one will be overwritten by the following) In_array ("Apple", $arr); Retrieve Apple Array_search ("Apple", $arr) in the array; Retrieve Apple in the array if there is a return key name Array_key_exists ("Apple", $arr); Retrieves whether the given key name exists in the array

Isset ($arr [Apple]): Retrieves whether the given key name exists in the array

An internal pointer to an array

Current ($arr); Returns the current cell POS ($arr) in the array; Returns the current cell key ($arr) in the array; Returns the key name of the current cell in the array prev ($arr); Rewind the internal pointer in the array back to a next ($arr); Moves the inner pointer in the array forward one end ($arr); Point the inner pointer in the array to the last unit reset ($arr); Points the inner pointer in the array to the first cell each ($arr); Constructs an array that returns a key name/value of the current element of the array and moves the array pointer forward one

List ($key, $value) =each ($arr); Gets the key name and value of the current element of the array

Conversions between arrays and variables

Extract ($arr); used to convert the elements in an array into variables into the current file, the key name as the variable name, and the value as the variable value. Note: (The second parameter is very important, can see the manual use) method of use echo $a;

Compact (VAR1,VAR2,VAR3); Create an array with the given variable name

Ii. segmentation and padding of arrays

Fragment of an array

Array_slice ($arr, 0, 3); You can take a paragraph out of the array, and this function ignores the key name

Array_splice ($arr, 0,3,array ("Black", "maroon")); You can take a paragraph out of an array, unlike the previous function, in which the returned sequence is removed from the original array.

Splitting multiple arrays

Array_chunk ($arr, 3,true); You can split an array into multiple, true to preserve the key name of the original array

Padding of arrays

Array_pad ($arr, 5, ' X '); Fills an array into a set length

Three, arrays and stacks

Array_push ($arr, "apple", "pear"); Presses one or more elements into the end of the array stack (into the stack), returning the number of elements in the stack

Array_pop ($arr); POPs the last element of the array stack (out of the stack)

Iv. Arrays and queues

Array_shift ($arr); The first element in the array is moved out and returned as a result (array length minus 1, other elements move forward one bit, number key name changed from zero technology, text key name unchanged)

Array_unshift ($arr, "a", array); inserts one or more elements at the beginning of the array

Five, callback function

Array_walk ($arr, ' function ', ' words '); Use the user function to process each member in the array (the third argument is passed to the callback function) Array_map ("function", $arr 1, $arr 2); Multiple arrays can be processed (when two or more arrays are used, their lengths should be the same) Array_filter ($arr, "function"); Using a 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-valued function (* The first value of an array)

Vi. Ordering of arrays

Sort by array of element values

Sort ($arr); Sort by order of small to large (the second parameter is sorted by what sort) and the array ordering Rsort ($arr) that ignores the key name; Sort by order of large to small (the second parameter is sorted by what sort) and the array ordering Usort ($arr, "function") that ignores the key name; Use a user-defined comparison function to sort the values in the array (there are two parameters in function, 0 for equality, positive for the first one greater than the second, negative for the first less than the second) to sort Asort ($arr) for arrays that ignore key names; Sort by order of small to large (the second parameter is sorted by what sort) and the array of reserved key names Arsort ($arr); Sort by order of large to small (the second parameter is sorted by what sort) the array of reserved key names

Uasort ($arr, "function"); Use a user-defined comparison function to sort the values in the array (function with two parameters, 0 for equality, positive for the first one greater than the second, negative for the first one less than the second) to sort the arrays of reserved key names

Sort arrays by key names

Ksort ($arr); Sort Krsort ($arr) in positive order by key name; Sort by key name in reverse order

Uksort ($arr, "function"); Use a user-defined comparison function to sort the key names in the array (there are two parameters in function, 0 for equality, positive for the first greater than the second, and negative for the first less than the second one)

Sort by natural sort

Natsort ($arr); Natural sort (ignores key names)

Natcasesort ($arr); Natural sort (ignores case, ignores key name)

Vii. Calculation of arrays

Sum of array elements

Array_sum ($arr); Sums all elements inside an array

Merging of arrays

Array_merge ($arr 1, $arr 2); Merge two or more arrays (the same string key name, followed by overwriting the previous, same numeric key name, followed by no overwrite operation, but appended to the back) "+" $arr 1+ $arr 2; Keep only the first one for the same key name (for more information, refer to: http://www.qianyunlai.com/blog/138.html)

Array_merge_recursive ($arr 1, $arr 2); Recursive merge operations, if the array has the same string key name, these values will be merged into an array. If a value is itself an array, it will be 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 append to the back

The difference set of an array

Array_diff ($arr 1, $arr 2); Returns an array of difference results

ARRAY_DIFF_ASSOC ($arr 1, $arr 2, $arr 3); Returns the array of difference results, and the key names are also compared

Intersection of arrays

Array_intersect ($arr 1, $arr 2); Returns an array of intersection results

ARRAY_INTERSECT_ASSOC ($arr 1, $arr 2); Returns an array of intersection results, with key names also being compared

Eight, other array functions

Range (0,12); Creates an array containing the specified range of cells Array_unique ($arr); The duplicate values in the array are removed, and the original key names Array_reverse ($arr, TRUE) are preserved in the new arrays; Returns an array in which the cell order is opposite the original array, and if the second argument is true, preserve the original key name//srand ((float) microtime () *10000000); Random seed Trigger Array_rand ($arr, 2); Randomly extracts one or more elements from an array shuffle ($arr); Disturb the order of the arrays

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