A full array of PHP operation methods

Source: Internet
Author: User
Tags array sort random seed shuffle

First, the basic functions of array operations
Array keys and values
array_values ($ arr); Get array values
array_keys ($ arr); get array keys
array_flip ($ arr); the values in the array are swapped with the key names (if there is a duplicate the previous one will be overwritten by the latter)
in_array ("apple", $ arr); retrieve apples in the array
array_search ("apple", $ arr); Retrieve apple in the array and return the key name if it exists
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


Internal pointer to an array
current ($ arr); returns the current cell in the array
pos ($ arr); returns the current cell in the array
key ($ arr); returns the key name of the current cell in the array
prev ($ arr); rewinds the internal pointer in the array
next ($ arr); move the internal pointer in the array forward by one
end ($ arr); point the internal pointer in the array to the last cell
reset ($ arr; points the internal pointer in the array to the first cell
each ($ arr); a constructed array that returns a key / value of the current element of the array and moves the array pointer forward by one
list ($ key, $ value) = each ($ arr); get the key and value of the current element of the array


Conversion between arrays and variables
extract ($ arr); used to convert the elements in the array into variables and import them into the current file, with the key name as the variable name and the value as the variable value
Note: (The second parameter is very important, you can use it in the manual) How to use echo $ a;
compact (var1, var2, var3); create an array with the given variable names


Segmentation and padding of arrays
Segmentation of an array
array_slice ($ arr, 0,3); can fetch a section from the array, this function ignores the key name
array_splice ($ arr, 0,3, array ("black", "maroon")); You can take out a section of the array, which is different from the previous function in that the returned sequence is deleted from the original array


Split multiple arrays
array_chunk ($ arr, 3, TRUE); can split an array into multiple, TRUE is the key name of the original array


Filling an array
array_pad ($ arr, 5, 'x'); pad an array to the specified length


Arrays and stacks
array_push ($ arr, "apple", "pear"); Push one or more elements to the end of the array stack (push) and return the number of elements on the stack
array_pop ($ arr); Pop (pop) the last element of the array stack


Arrays and queues
array_shift ($ arr); the first element in the array is shifted out and returned as the result (the length of the array minus 1, the other elements are moved forward by one, the number key name is changed from zero technology, and the text key name is unchanged)
array_unshift ($ arr, "a", array (1,2)); insert one or more elements at the beginning of the array


Five, callback function
array_walk ($ arr, 'function', 'words'); use a user function to process each member of the array (the third parameter is passed to the callback function function)
array_mpa ("function", $ arr1, $ arr2); can handle multiple arrays (when using two or more arrays, they should be the same length)
array_filter ($ arr, "function"); Use a callback function to filter each 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 of the array will remain unchanged.
array_reduce ($ arr, "function", "*"); converted to a single-valued function (* is the first value of the array)


Sorting arrays
Sorting an array by element value
sort ($ arr); sort in ascending order (the second parameter is how to sort) ignore array sorting of key names
rsort ($ arr); Sorting in descending order (the second parameter is how to sort) Array sorting that ignores key names
usort ($ arr, "function"); use a user-defined comparison function to sort the values in the array (the function has two parameters, 0 is equal, a positive number means the first is greater than a second, and a negative number means the first One less than the second) sort of arrays ignoring key names
asort ($ arr); sort in ascending order (the second parameter is how to sort) array sorting that preserves key names
arsort ($ arr); sort from big to small (the second parameter is how to sort) array sorting that keeps key names
uasort ($ arr, "function"); use a user-defined comparison function to sort the values in the array (the function has two parameters, 0 is equal, a positive number means the first is greater than a second, and a negative number means the first An array sort of less than the second) preserving key names


Sorting an array by key name
ksort ($ arr); sort by key order
krsort ($ arr); sort in reverse order by key name
uksort ($ arr, "function"); use a user-defined comparison function to sort the keys in the array (there are two parameters in the function, 0 means equal, a positive number means the first is greater than a second, and a negative number means The first is less than the second)


Natural ordering
natsort ($ arr); natural sort (ignore key names)
natcasesort ($ arr); natural sorting (ignore case, ignore key names)


Calculation of arrays
Sum of array elements
array_sum ($ arr); sums all elements inside the array


Merge arrays
array_merge ($ arr1, $ arr2); Merge two or more arrays (the same string key name, the latter overwrites the previous, the same number key name, the latter will not be overwritten, but appended to the back)
"+" $ Arr1 + $ arr2; keep only the latter for the same key name
array_merge_recursive ($ arr1, $ arr2); Recursive merge operation, 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 by 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 to the end


Difference of arrays
array_diff ($ arr1, $ arr2); returns an array of difference results
array_diff_assoc ($ arr1, $ arr2, $ arr3); Returns the array of difference results, and compares the key names


Intersection of arrays
array_intersect ($ arr1, $ arr2); returns an array of intersection results
array_intersect_assoc ($ arr1, $ arr2); returns the array of intersection results, and the key names are also compared


Eight other array functions
range (0,12); create an array containing the cells of the specified range
array_unique ($ arr); Remove duplicate values from the array. The original key name will be retained in the new array.
array_reverse ($ arr, TRUE); returns an array with the element order reversed from the original array. If the second parameter is TRUE, the original key name is retained.
// srand ((float) microtime () * 10000000); Random seed trigger
array_rand ($ arr, 2); randomly remove one or more elements from the array
shuffle ($ arr); shuffle the order of the array


Nine, return an array according to certain rules


implode (string glue, array pieces);

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.