Home >Backend Development >PHP Tutorial >Summary of PHP common functions (array part) shared version will have some errors_PHP tutorial

Summary of PHP common functions (array part) shared version will have some errors_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:47:36961browse

Function name Purpose Format Input Output Operation

Array generation and conversion:

array() generates an array array array(mixed [...]) array value or, key => value an array variable None

array_combine() generates an array, using the value of one array as the key name and the value of the other array as the value array array_combine(array $keys, array $values) $keys is the array that provides the key, and $values ​​is the array that provides the value. Array None

range() creates an array of specified range units array range(mix $low,mix $high,[num $step]) $low minimum value, $high maximum value, $step step size Synthetic array None

compact() creates an array, including their variable names and values ​​arr compact(mix $varname,[,mix $...]) various variables, and the array returns an array consisting of variable names as keys and variable values ​​as values. It can also be a multi-dimensional array. It will be processed recursively without

array_fill() generates an array array with a given value array_fill(int $start, int $num, mix $value) Starting with the key of $start, $num elements, $value is the filling content Returns the completed array None

Array merging and splitting:

array_chuk() divides an array into multiple specified arrays array_chunk(arr $input,int $size[,bool $preserve_keys]) $input is the array to be operated on, $size contains several elements in each copy, $preserve_keys is Boolean, whether to preserve the keys The divided multi-dimensional array has no

array_merge() merges one or more arrays. If the keys of the elements are the same, the previous ones will be overwritten, and the sequence of indexes will not be overwritten, only arr will be automatically increased array_merge(arr $arr1[, arr $arr2,...] ) $arr1, etc. return the completed array for the operated array. None

Array comparison operations:

array_diff() Calculate the difference of arrays array array_diff(arr $arr1,arr $arr2[,...]) Input two or more arrays and compare the differences of several array element values ​​None

array_intersect() Calculate the intersection of arrays array array_intersect(arr $arr1,arr $arr2[,...]) Input two or more arrays Compare the intersection of several array element values ​​None

Array search and replace operation:

array_search() Search the given value from the array and return the key name mix array_search(mix $need,arr $arr[bool $stric]) $need is the search string, $arr is the target array, and $stric is true. If the matching variable type is successful, the key name will be returned. If it fails, it will return false. None

array_slice() Take out a segment arr array_slice(arr $array,int $offset[,int $length[bool $p_key]) from the array $array target array;$offset offset;$length interception length;$p_key:true Keep the key name. The key name $array target array will be reassigned by default; if $offset is non-negative, it will start from this offset. If it is a negative value, it will start from this far from the end; if $length is positive, there will be this in the sequence. Multi-unit, if not negative, the sequence ends at this far from the end. None

array_splice() Remove part of the array and replace arry with other values ​​array_splice(arr &$input,int $offset[,int $leng[,arr $rep]]) $input is the target array, $offset is the offset, $ If leng does not exist, all elements from $offset to the end will be removed. If it is a positive value, so many units will be removed. If leng is a negative value, so many units from $offk to the end of $leng will be removed. The replaced part will replace the original array. Note that the key names in the array are not retained after replacement. If $rep is given, the original array will be replaced by the new array elements

array_sum() calculates the sum of all values ​​in the array number array_sum(arr $array) target array returns the sum None

in_array() Finds whether a certain value exists in the array, case-sensitive bool in_array(mix $need,arr $array[,bool $str]) Searches $need in $array, if $str is true, the check type is also To match. Return true if found, false if not found None

array_key_exists() Check whether the given key name exists bool array_key_exists(mix $key, arr $serch) $key searches for the key, $serch target array returns whether it is found None

Array pointer operations:

key()

current() alias pos()

next()

prev()

end()

reset()

list() assigns the values ​​in the array to the variable void list(mix $var1[,$var2...])=arr $array

array_shift()

array_unshift()

array_push() The array finally pushes multiple elements int array_push(arr &$array,mix $var1[,$var2...]) &$array target array,$var1.. returns 1 for success when adding the value, and 0 for If failed, directly operate on the target array

array_pop() The last element of the array pops out mix array_pop(arr &$array) &$array is the target array, returns the value of the popped element, and directly operates on the target array

Array key value operations:

shuffle() shuffles the array and retains the key names bool shuffle(arr &$array) inputs the array and returns true. The key names will not change after shuffling the order

count()

array_flip() exchanges the keys and values ​​​​in the array arr array_flip(arr $trans) $trans is the operated array Returns the completed array None

array_keys() returns all the keys of the array to form an array arr array_keys(arr $input[,mix $search[,bool $str]]) If $search has a value, it will only return the key name corresponding to this value. If $str is true, , and also compare types to return an array composed of key names without

array_values() returns all the values ​​in the array to form an array array array_values ​​(array $input)

array_reverse() returns an array with the elements in the reverse order arr array_reverse(arr $input[,bool $key]) Input the target array, if $key is true, the original key name is retained. An array in the reverse order None

array_count_values() counts the number of occurrences of all values ​​in the array array array_count_values(array $input) $input is the array being operated on, using the value of input as the key and the number of occurrences as the value. None

array_rand() randomly extracts one or more elements from the array, note the key name!!! mix array_range(arr $input[,int $num]) $input is the target array, $num is the number of elements extracted and returned The key of the element, if it is one, it is a key name; if there are multiple elements, these keys will be used as values ​​to form an index array and return None

each()

array_unique() removes duplicate values ​​from the array, first sorts the values, then only retains the first encountered key name, and then ignores the remaining key names array array_unique(arr $array) Enter the target array key name and retain it Variable returns an array with no duplicate values ​​

Sort an array:

sort() sorts the array (from low to high), without retaining the key name bool sort(arr &$array[,int $flag]) The target array returns true if successful, false if failed. Rearranges the array elements and changes the keys at the same time. First name

rsort() sorts the array in reverse order (from high to low), without retaining the key name bool rsort(arr &$array[,int $flag]) The target array returns true if successful, false if failed. Rearranges the array elements and changes them at the same time. Key name

asort() Sort the array and keep the index relationship bool asort(arr &$array[,int $flag]) Target array Return true if successful, false if failed Sort the array and keep the original index or key

arsort() sorts the array in reverse order, maintaining the index relationship

ksort() Sorts the array by key name bool ksort(arr &$array[int $flag]) Target array Returns true if successful, false if failed Sorts the key name and retains the key-value correspondence

krsort() sorts the array in reverse order by key name

natsort() ""Natural algorithm""sorts the array, maintaining the key-value relationship" bool natsort(arr &$array) The target array returns true if successful, false if failed. Sorts the values ​​naturally, retaining the key-value relationship

natcasesort() natural sorting, case-insensitive bool natcasesort(arr &$array) target array returns true on success, false on failure, performs natural sorting of values ​​without case sensitivity, and maintains key-value correspondence

Excerpted from Battlefield Diary_LAMP Band of Brothers

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478505.htmlTechArticleFunction name purpose format input and output operation array generation and conversion: array() generates an array array array(mixed [. ..]) array value or, key = value an array variable without array_combine(...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn