PHP Array Function classification
basic functions of array manipulation
1, the key name and value of the array
array_values ($arr); Get the value of an 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 are duplicates, the previous one will be overwritten by the following)
In_array ("Apple", $arr); Retrieve Apple in an array
array_search ("Apple", $arr); Retrieve Apple in an 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
2. Internal pointers to arrays
current ($arr); Returns the 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); Rewind 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), 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
3. Conversion between arrays and variables
extract ($arr); used to convert an element in an array into a variable and import it into the current file, the key name as the variable name, and the value as the variable value
Compact (VAR1,VAR2,VAR3); Create an array with the given variable name
ii. Segmentation and padding of arrays
1. Segmentation of Arrays
array_slice ($arr, 0, 3); You can take a paragraph out of the array, 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 .
2. 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
3. Padding of arrays
Array_pad ($arr, 5, ' X '); fills an array into a set length
three, arrays and stacks
Array_push ($arr, "apple", "pear"), pressing 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); 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 remains unchanged
array_reduce ($arr, "function", "*"), converted to a single-valued function (* The first value of an array)
vi. Ordering of arrays
1. Sorting by array of element values
Sort ($arr), sorted by order of small to large (the second argument is sorted by what sort), and the array ordering of the key names is ignored
Rsort ($arr); Sort by order of large to small (the second argument is sorted by what sort) ignores the array ordering of the key names
Usort ($arr, "function"); Sorts the values in the array using a user-defined comparison function (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 the array with the key name omitted
Asort ($arr); Sort by the order of small to large (the second argument is sorted by what way) 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"); Sorts the values in the array using a user-defined comparison function (two parameters in function, 0 for equality, positive for the first one greater than the second, negative for the first one less than the second) to sort the array of reserved key names
2. Sorting by key name Array
Ksort ($arr); Order by key name
Krsort ($arr); reverse order by key name
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 one less than the second one)
3. Sort by natural sort
Natsort ($arr); natural sort (Ignore key name)
Natcasesort ($arr); natural sort (ignoring case, ignoring key name)
vii. Calculation of arrays
1. Sum of array elements
array_sum ($arr); sums all elements inside an array
2. Merging of arrays
Array_merge ($arr 1, $arr 2); merges two or more arrays (the same string key name, followed by overwriting the previous, same number key name, followed by no overwrite operation, but appended to the back) "+" $arr 1+ $arr 2; For the same key name, keep only the latter one
array_merge_recursive ($arr 1, $arr 2); recursive merge operations, if the array has the same string key name, the 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
3, 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
4. 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, and the key names are also compared
eight, other array functions
range (0,12); Create an array containing the specified range of cells
Array_unique ($arr); Removes duplicate values from the array, preserving the original key names in the new arrays
Array_reverse ($arr, TRUE); Returns an array whose cell order is the opposite of the original array, if the second argument is TRUE to preserve the original key name
//srand (float) microtime () *10000000) random seed trigger
Array_rand ($arr, 2); Randomly remove one or more elements from an array
Shuffle ($arr); disturb the order of the array