PHP array common function classification

Basic functions for array operations

Array keys and values array_values($arr); Array_keys (array_keys)$arr); Get the array's key name array_flip($arr); Values in arrays are interchanged with key names (duplicates are overwritten) in_array("apple".$arr); Retrieves the Apple array_search("apple".$arr); Retrieves apple from array, returns key name array_KEY_exists ("apple".$arr); Retrieves whether the given key name is present in the arrayisset($arr[apple]): Retrieves whether the given key name exists in the array of the array's internal pointer current($arr); Returns the current unit pos($arr); Returns the current cell key in the array ($arr); Returns the key name of the current cell in the array prev($arr); Rewind the internal pointer to the array by a bit next($arr); Moves the internal pointer in the array forward one end($arr); Set the internal pointer in the array to the last cell reset($arr; Set the internal pointer in the array to the first unit each($arr); Returns a constructed array of key names/values for the current element of the array and moves the array pointer one bit forwardlist($key.$value)=each($arr); Extract (); extract();$arr); This method is used to convert elements of an array into variables and import them into the current file, using the key as the variable name and the value as the variable valueecho $a; compact(var1,var2,var3); Creates an array with the given variable nameCopy the code

Fragment and fill an array

Array_slice ($arr.0.3); To retrieve a segment from an array, this function ignores the key array_splice($arr.0.3.array("black"."maroon")); You can take a segment out of an array. The difference is that the returned sequence is removed from the array and split multiple arrays array_chunk($arr.3.TRUE); You can split an array into multiple parts,TRUEArray_pad (array_pad(array_pad))$arr.5.'x'); Fills an array to a specified lengthCopy the code

Arrays and stacks

array_push($arr."apple"."pear"); Pushes one or more elements to the end of the array stack. Returns the number of elements pushed. Array_pop ($arr); Eject the last element of the array stackCopy the code

Array and queue

array_shift($arr); The first element in the array is removed and returned as a result (array length minus1, other elements move forward one bit, numeric key names change from zero technique, text key names remain unchanged) array_unshift($arr."a".array(1.2)); Inserts one or more elements at the beginning of an arrayCopy the code

The callback function

array_walk($arr.'function'.'words'); Each member of the array is processed using a user function (the third argument is passed to the callback function)function)array_mpa("function".$arr1.$arr2); Can handle multiple arrays (when two or more arrays are used, they should be the same length) array_filter($arr."function"); Filter each element in the array using a callback function if the callback function isTRUE, the current element of the array is included in the returned result array, and the array's key name remains unchanged. Array_reduce ($arr."function"."*"); To a single-valued function (* is the first value of the array)Copy the code

Sort an array

Sort an array by element value$arr); Sort from smallest to largest (the second argument is sort by what method)$arr); Sort from largest to smallest (the second argument is how to sort)$arr."function"); Sort the values in an array using a user-defined comparison function (functionIn, 0 means equal, positive means the first is greater than the second, and negative means the first is less than the secondasort($arr); Sort the order from smallest to largest (the second argument is how to sort) by reserving the array names arsort($arr); Sort the order from largest to smallest (the second parameter is how to sort) by reserving the key name of the array sort uasort($arr."function"); Sort the values in an array using a user-defined comparison function (functionA positive number means that the first is greater than the second, and a negative number means that the first is less than the secondksort($arr); Sort by key name krsort($arr); Sort by key in reverse order uksort($arr."function"); Sort the keys in an array using a user-defined comparison function (functionWhere 0 means equal, positive means greater than the second, and negative means less than the secondnatsort($arr); Natural sort(ignore key names) natCasesort ($arr); Natural sorting (ignore case, ignore key names)Copy the code

7, array calculation

Array_sum ($arr); Array_merge ($arr1.$arr2); Merges two or more arrays (same string key names, same numeric key names, same string key names, same numeric key names, not overwrite, but append to) "+"$arr1+$arr2; Keep only the last array_merge_recursive for the same key name ($arr1.$arr2); Recursive merge operation, 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 is merged into another array by the corresponding key name. When arrays have the same array key name, the latter value will not overwrite the original value, but will be appended to the difference set array_diff($arr1.$arr2); Array_diff_assoc (array_diff_ASsoc ($arr1.$arr2.$arr3); Array_intersect (array_intersect());$arr1.$arr2); Array_intersect_assoc (array_intersect_ASsoc)$arr1.$arr2); Returns an array of intersection results, and also compares key namesCopy the code

Other array functions

range(0.12); Creates an array array_UNIQUE ($arr); If duplicate values are removed from the array, the new array will retain the original key name array_reverse($arr.TRUE); Returns an array of cells in reverse order if the second argument isTRUEKeep the original key name//srand((float)microtime()*10000000); Random seed trigger
array_rand($arr.2); Shuffle () Randomly fetches one or more elements from an array$arr); Shuffle the order of the arrayCopy the code

The above content hopes to help you, more free PHP factory PDF, PHP advanced architecture video materials, PHP wonderful good article can be wechat search concerns: PHP open source community

2021 Jinsanyin four big factory interview real questions collection, must see!

Four years of PHP technical articles collation collection – PHP framework

A collection of four years’ worth of PHP technical articles – Microservices Architecture

Distributed Architecture is a four-year collection of PHP technical articles

Four years of PHP technical essays – High Concurrency scenarios

Four years of elite PHP technical article collation collection – database