Common functions for php arrays

Because there are so many array functions, many of them are labeled in English. There are also some array functions that are less commonly used.

Therefore, we have summarized the most commonly used array functions with you, so that you can remember which function you must mention at any time. Use directly.

Please use these functions several times against the examples in the manual. Requirements: dictation level.

In the future, when reading and writing any code, there is no need to reflect for a long time. Using these functions directly is the basic quality of a programmer.

These functions are also the most frequently asked questions in basic interviews.


##array_combine( )Generate an array, using the value of one array as the key name and the value of the other array as the valuerange() Create and return an array containing the specified range of elements. ##compact()array_fill()##array_chunk( )Split an array into new array blocks Split two or Combine multiple arrays into one arrayRemove a segment of value from the array based on conditions and returnReturns the difference array of two arraysSearch for the given value in the array, and if successful, return the corresponding key nameRemove part of the array and replace it with other values##array_sum()in_array()array_key_exists()shuffle() count()##array_values()Returns all the values ​​in the array to form an arrayarray_reverse()Returns an array with the elements in reverse order##array_rand() Randomly draw from the array One or more elements, note the key name##array_unique()sort()rsort()asort()##arsort()Reverse the array Sort, maintain index relationshipksort()Sort the array by key namekrsort() Sort the array in reverse order by keyUse the natural order algorithm to sort the elements in the arrayNatural sorting, not case sensitive
Function nameFunction


Create an array consisting of the variables taken by the parameters
Generate an array with the given value

##array_merge()

array_slice()
##array_diff()
array_search()
array_splice()

Calculate the sum of all values ​​in the array

Check whether a certain value exists in the array

Check whether the given key name or index exists in the array

Shuffle the array and keep the key values

Calculate the number of cells in the array or the number of attributes in the object
##array_flip()Return an array with key values ​​reversed
array_keys()Returns all the keys of the array to form an array


##array_count_values() Count the number of occurrences of all values ​​in the array

Delete duplicate values ​​and return the remaining array
Sort the values ​​of the given array in ascending order, without retaining key names
Reverse sort the array without retaining key names
Sort the array and keep the index relationship



##natsort()
##natcasesort()
array_filter()Remove empty elements or predetermined elements in the array
extractChange keys into variable names and values ​​into variable values


Continuing Learning
||
<?php $arr = array(1,2,3,4,5); function fan($arr) { $temp = array(); foreach($arr as $v) { array_unshift($temp,$v); } return $temp; } print_r(fan($arr)); echo '<hr />'; ?>
submitReset Code