PHP Array Operation method Daquan

Source: Internet
Author: User
Tags array sort compact random seed shuffle
If you are still struggling to find the PHP array operation method, you must not miss this article, the most complete PHP array operation method, I hope to be helpful.

the key name and value of the basic function Array array_values ($ARR), get the value of the array Array_keys ($arr), get the key name of the array array_flip ($arr); The values in the array are interchanged with the key names (if there are duplicates that are later overwritten) In_array ("Apple", $arr), Applearray_search ("Apple", $arr) are retrieved in the array, and Apple is retrieved 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

the inner pointer of the array is current ($arr), returns the present cell POS ($arr) in the array, returns the current cell key ($arr) in the array, returns the key name Prev ($arr) of the current cell in the array, and the internal pointer in the array back to a next ($arr); Move the inner pointer in the array forward one end ($arr), point the inner pointer in the array to the last cell reset ($arr; point the inner pointer in the array to the first cell each ($arr); the constructed array that returns a key name/value for 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

the conversion between an array and a variable extract ($arr); used to convert an element in an array into a variable into the current file, the key name as the variable name, and the value as the variable value Note: (The second parameter is important, you can see the manual use) echo $a; compact ( VAR1,VAR2,VAR3); Create an array with the given variable name

The fragment of the array and the segment of the padding array array_slice ($arr, 0, 3); You can take a paragraph out of the array, which 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.

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

the array is filled with array_pad ($arr, 5, ' X '); Fills an array to the length of the set

Three, array and stack Array_push ($arr, "apple", "pear"), pressing one or more elements into the end of the 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)

Array and queue Array_shift ($arr); The first element in the array is moved out and returned as the result (the array length minus 1, the other elements move forward one bit, the number key name is changed from zero technology, the text key name is unchanged) Array_unshift ($arr, "a", Array ( )); inserts one or more elements at the beginning of the array

The callback function Array_walk ($arr, ' function ', ' words ') is processed using the user function for each member in the array (the third argument is passed to the callback function) Array_mpa ("function", $arr 1, $arr 2); You can work with multiple arrays (they should be the same length when using two or more arrays) 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 is left unchanged array_reduce ($arr, "function", "*"), and converted to a single-valued function (* The first value of a group)

The sorting of arrays sorts ($arr) by the array of element values , sorted by the order of small to large (the second argument is sorted by what sort), and the array ordering Rsort ($arr) that ignores the key names; Sorts from large to small (the second argument is sorted by what sort) the array ordering Usort ($arr, "function") that ignores the key names, and the values in the arrays are sorted using a user-defined comparison function (there are two parameters in function, 0 for equality, A positive number indicates that the first one is greater than the second, the negative number represents the first less than the second, and the array ordering Asort ($arr) that ignores the key name, sorted by the order of the small to large (the second argument is sorted by what way), and the array ordering Arsort ($arr) that retains the key name; Sorts from large to small (the second parameter is sorted by what sort) the array of reserved key names is sorted Uasort ($arr, "function"), and the values in the arrays are sorted using a user-defined comparison function (there are two parameters in function, 0 for equality, A positive number indicates that the first one is greater than the second, and the negative number indicates that the first one is less than the second one. Sort the array

Sorts Ksort ($arr) by key name, sorts Krsort ($arr) by key name, sorts Uksort in reverse order of key name ($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)

Natural Sorting Natsort ($arr); natural sort (Ignore key name) Natcasesort ($arr); natural sort (ignores case, ignores key name)

The sum of Array_sum ($arr) of the array elements , and the sum of all the elements inside the arrays

the merging of the arrays Array_merge ($arr 1, $arr 2), merging two or more array (the same string key name, followed by overwriting the previous, the same number key name, followed by no overwrite operation, but appended to the back) "+" $arr 1+ $arr 2; For the same key name only after one array_merge_recursive ($arr 1, $arr 2) is retained; 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 the 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

the intersection of the array array_intersect ($arr 1, $arr 2), returns the intersection result array Array_intersect_assoc ($arr 1, $arr 2), returns an array of intersection results, and the key names are also compared

The other Array function range (0,12), creates an array Array_unique ($arr) containing the specified range of cells, removes the duplicate values in the array, and retains the original key names Array_reverse ($arr, TRUE) 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 shuffle ($arr) from an array, disrupting the order of the arrays this class of functions allows you to manipulate arrays and interact with them in a variety of ways. The essence of an array is to store, manage, and manipulate a set of variables. PHP supports one-dimensional and multidimensional arrays that can be created by the user or created by another function. There are certain database processing functions that can generate arrays from database queries, and some functions return arrays.

array_change_key_case-returns an array of string key names that are all lowercase or uppercase array_chunk-splits an array into multiple array_combine-creates an array with the value of an array as its key name. The value of another array as its value array_count_values-the number of occurrences in the statistic array array_diff_assoc-the difference set of the computed array with the index check array_diff_key-the difference set of the computed array using the key name Array_ diff_uassoc-uses the user-supplied callback function to do an index check to calculate the difference set of the array array_diff_ukey-use the callback function to compare the difference set of the computed array array_diff-computes the difference set of the array Array_fill_keys-fill an Array with values, specifying keysarray_fill-fills the array with the given value array_filter-filters the cells in the array with a callback function array_flip-the keys and values in the array Array_ intersect_assoc-with index check computes the intersection of an array array_intersect_key-using the key name to compare the intersection of the computed array array_intersect_uassoc-the intersection of the computed array with the index check, Compare indexes with callback functions array_intersect_ukey-the intersection of the array by comparing the key names with the callback function array_intersect-computes the intersection of the array array_key_exists-checks whether the given key name or index exists in the array _keys-returns all the key names in the array array_map-the callback function to the cell of the given array array_merge_recursive-recursively merges one or more arrays array_merge-merge one or more arrays Array_ multisort-to sort multiple arrays or multidimensional arrays array_pad-fills an array to a specified length with a value array_pop-the last cell of the array pops up (out of the stack) array_product-computes the product of all values in the array array_push- Presses one or more cells into the end of the array (into the stack) array_rand-randomly extracts one or more cells from an array array_reduce-uses a callback function to iteratively simplify the array to a single value array_reverse-returns an array of cells in reverse orderarray_search-searches the array for the given value and, if successful, returns the corresponding key name array_shift-the cell at the beginning of the array array_slice-a paragraph from the array array_splice- Remove part of the array with other values instead of array_sum-computes the difference set of all the values in the array with the array_udiff_assoc-index check computed array, using the callback function to compare the data array_udiff_uassoc-with the index check the difference set of the computed array, Compare data and indexes with callback functions array_udiff-use callback functions to compare data to calculate the difference set of arrays array_uintersect_assoc-the intersection of computed arrays with index checks, and to compare data with callback functions array_uintersect_uassoc- With index check the intersection of the computed array, use the callback function to compare the data and index array_uintersect-compute the intersection of the array, use the callback function to compare the data array_unique-to move the duplicate values in the divisor group array_unshift- Inserts one or more cells at the beginning of the array array_values-returns all the values in the array array_walk_recursive-the user function is applied recursively to each member of the arrays array_walk-the user function is applied to each member of the group array- Create a new array arsort-the arrays to be reversed and keep the index relationship asort-the array, and keep the index relationship compact-create a list of variables, including the variable names and their values count-calculate the number of cells in the array or the number of properties in the object current- Returns the current cell in the array each-returns the current key/value pair in the array and moves the array pointer forward one step end-the inner pointer of the array to the last cell extract-import the variable from the array into the current symbol table in_array-Check the array for a value key- Get a key name from an associative array krsort-arrays are reversed by key name ksort-arrays are sorted by key name list-assigns the values in the array to some variables natcasesort-uses the "natural sort" algorithm to sort the uppercase and lowercase letters of the group using " Natural sort algorithm Array sort next-moves the internal pointer in the array forward one pos-current () alias prev-returns an array's internal pointer to a range-creates an array with the specified range of cells reset -Points The inner pointer of the array to the first cell rsort-the array shuffle-the alias of the Sizeof-count () sort-The order of the arrays uasort-use a user-defined comparison function to sort the values in the array and keep the index associated UKSO rt-use a user-defined comparison function to sort the key names in an array usort-use a user-defined comparison function to sort the values in the array

Using arrays in the previous chapters, the variables we introduced are scalar variables that store only a single data. An array is a variable that can store a set or series of values. An array can have a number of elements. Each element has a value, such as text, a number, or another array. An array containing other arrays is called a multidimensional array.

  • Related Article

    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.