Php array operations

Source: Internet
Author: User
Tags array sort random seed
Php array operations
If you are still struggling to find the php array operation method, you must not miss this article, the most comprehensive php array operation method, and hope to help you.

I. basic functionsKey name and value of the arrayArray_values ($ arr); obtain the array value array_keys ($ arr); obtain the array key name array_flip ($ arr ); 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 arrayCurrent ($ 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 variablesExtract ($ 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 arraysArray segmentationArray_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 arraysArray_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 fillingArray_pad ($ arr, 5, 'x'); fill an array to the specified length.

3. array and stack array_push ($ arr, "apple", "pear"); push 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. array and line-up 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, and other elements are moved forward by one, the number key name is changed from zero, and the text key name remains unchanged.) array_unshift ($ arr, "a", array (); insert one or more elements at the beginning of the array

5. callback function array_walk ($ arr, 'function', 'Word'); use the user function to process each member in the array (the third parameter is passed to the callback function) array_mpa ("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 will be included in the returned result array, the key name of the array remains unchanged. array_reduce ($ arr, "function", "*") is converted to a single-value function (* is the first value of the array)

6. sorting arraysSort arrays by element valuesSort ($ 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 nameKsort ($ 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 sortingNatsort ($ arr); natural sorting (ignore key names) natcasesort ($ arr); natural sorting (ignore case sensitivity, ignore key names)

VII. array calculationSum of array elementsArray_sum ($ arr); calculates the sum of all elements in the array.

Array mergingArray_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 setArray_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 intersectionArray_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) containing 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 ); shuffle ($ arr) is randomly taken from an array. this class of functions disrupt the order of arrays. this class of functions allows you to operate arrays and interact with them in multiple ways. The essence of arrays is to store, manage, and operate a group of variables. PHP supports one-dimensional and multi-dimensional arrays, which can be created by a user or by another function. Some specific database processing functions can generate arrays from database queries, and some functions return arrays.

Array_change_key_case-array array_chunk whose return string key names are all lowercase or uppercase-split an array into multiple array_combine-create an array and use the value of an array as its key name, the value of the other array is used as its value array_count_values-count the number of times all values in the array appear array_diff_assoc-array_diff_key, the difference set of the calculated array with the index check-array_diff_uassoc- use the callback function provided by the user for index check to calculate the array's difference set array_diff_ukey-use the callback function to compare the calculated array's difference set array_diff-calculate the array's difference set array_fill_keys-Fill array with values, specifying keysarray_fill-fill the array array_filter with the given value-use the callback function to filter the elements in the array array_flip-exchange keys and values in the array array_intersect_assoc-intersection array_intersect_key with the index calculates the intersection of arrays. array_intersect_uassoc-calculates the intersection of arrays with an index check, use the callback function to compare the index array_intersect_ukey-use the callback function to compare the key names to calculate the array intersection array_intersect-calculate the array intersection array_key_exists-check whether the given key name or index exists in the array array_keys all key names in the array, array_map, apply the callback function to the units of the given array. array_merge_recursive-recursively merges one or more arrays, array_merge-merges one or more arrays, array_multisort-sort arrays or multi-dimensional arrays array_pad-fill the array with values to the specified length array_pop-bring up the last unit of the array (output stack) array_product-calculate the product of all values in the array array_push-push one or more units to the end of the array (into the stack) array_rand-randomly retrieve one or more units from the array. array_reduce-use the callback function to iteratively simplify the array to a single value. array_reverse-return an array with the opposite unit order. array_search-in the array search for a given value, if it succeeds, return the corresponding key name array_shift-remove the unit starting with the array from the array array_slice-remove part of the array and replace array_sum with other values to calculate the array. the difference set between all values and the array_udiff_assoc-array with index check, use the callback function to compare the data array_udiff_uassoc-use the index check to calculate the array's difference set, use the callback function to compare data with the index array_udiff-use the callback function to compare data to calculate the array_uintersect_assoc of the array's difference set-with the index check to calculate the intersection of the array, use the callback function to compare the data array_uintersect_uassoc-with the index check to calculate the intersection of the array, and use the callback function to compare the intersection of the data and the index array_uintersect-to calculate the array, use the callback function to compare data array_unique-Remove repeated values in the array array_unshift-insert one or more units at the beginning of the array-return all values in the array array_walk_recursive-recursively returning each member in the array application User function array_walk-apply user function array to each member in the array-create an array arsort-reverse sort the array and maintain the index relationship asort-sort the array and maintain the index relationship compact -Create an array, including variable names and their values count-calculate the number of units in the array or the number of attributes in the object current-returns the current unit each in the array-returns the current key/value pair in the array and returns the array pointer move one step forward end-direct the internal pointer of the array to the last unit extract-import the variable from the array to the current symbol table in_array-check whether a value key exists in the array-from the associated array obtain the key name krsort-sort the array by key name in reverse order ksort-sort the array by key name in list-assign the value in the array to some variables natcasesort-use the "natural sorting" algorithm sort the array by case-insensitive letters natsort-sort the array by using the "natural sorting" algorithm next-move the internal pointer in the array to a position pos-current () prev-returns the internal pointer of the array to one range-creates an array reset containing the specified range unit-points the internal pointer of the array to the first unit rsort-sorts the array in reverse order shuffle -disrupt the array sizeof-count () alias sort-sort the array uasort-sort the values in the array using the user-defined comparison function and keep the index associated uksort-use the user-defined comparison function to the key name in the array sort by usort-sort the values in the array using the user-defined comparison function

Using arrays in the previous section, we described variables as scalar variables, which can only store a single data. An array is a variable that can store a group or a series of values. An array can have many elements. Each element has a value, such as text, number, or another array. An array containing other arrays is called a multi-dimensional 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.