PHP manipulates array related functions

  • 2020-03-31 21:31:52
  • OfStack

Ange ($low, $high), the range ($low, $, $step); // create an array of sequential values such as range(1,4), range(1,2,3,4), and range('a','z')

Each ($array) returns the current element of the array in order, and sets the next element to the current element.

Reset ($array) resets the current element of the array to the beginning of the array

List () can be used to break an array into a series of values, such as list($a,$b)=each($array)

The shuffle ($array), array_rand ($arg, $num_req); Sort the array randomly

Array_reverse ($input),array_reverse($input, $preserve_keys) returns the reverse sort of the original array

Sort ($array); Sort an array



PHP array is an important concept, it contains a large number of functions, convenient for people to develop... Now its array classification, in order to facilitate the query and application.
Let's start with the definition of a PHP array... PHP array contains two items,key and value, and the corresponding value can be obtained through key, in which key can be numeric and associated, such as $array[0],$array[one]...
Create an array
Array declarations in PHP are slightly different from those in other languages, but can be declared as one-dimensional, two-dimensional, three-dimensional, and multidimensional, for example
$array[0] = 1,$array = array(1,2,3); One dimensional array, which contains only three values, is a numeric array. $array[0] can be used as a reference to represent 1. When creating a numeric array, the index can be omitted.
 
$array = array( 
1 =>  " one " , 
2 =>  " two " , 
3 =>  " three " , 
4 => array( 
 " one "  => 1, 
 " two "  => 2, 
 " three "  => 3 
) 
); 

A two dimensional array, which is also an associative array, can be referenced by $array[4][" one "] to represent 1.
Three dimensions above and so on...
To create a batch array, use the following function:
Array range (mixed low, mixed high [, number step])
$array = range (1, 6); On behalf of the array (6);
$array = range (a, f); On behalf of the array (a, b, c, d, e, f);

The output array
PHP output array of functions have more, commonly used
Bool print_r (mixed expression [, bool return])
Void var_dump (mixed expression [, mixed expression [,...]])
Echo,print,printf can all print a single array.

The test array
Sometimes we need to determine whether a variable is an array, we can use:
Bool is_array (mixed var)

Add or remove array elements
The array declaration is not static, may be added to the array to delete to carry out in-depth operations:
Int array_push (array &array, mixed var [, mixed...]] ) to push one or more cells into the end of an array. The length of the array increases according to the number of pushed variables, such as array_push($array,$var).
Mixed array_pop (array &array) pops the last element of the array (out of the stack) and resets the pointer to the array after the end
Mixed array_shift (array &array) returns the first element of the array.
Int array_unshift (array &array, mixed var [, mixed...] Insert one or more cells at the beginning of the array
Array array_pad(array input, int pad_size, mixed pad_value) fills the array to the specified length with a value, such as array_pad($array,3,$var);

Locate array element
Bool in_array (mixed needle, array haystack [, bool strict]) checks for the presence of a value in the array
Array array_keys (array input [, mixed search_value [, bool strict]]) returns all the key names in the array, reorganized into a new array
Bool array_key_exists (mixed key, array search) checks whether the given key exists in the array.
Array array_values (array input) returns all values in the array
Mixed array_search (mixed needle, array haystack [, bool strict]) searches an array for a given value and returns the key on success.

Through the array
There are many functions in PHP that get keys and values
Mixed key (array &array) gets the key name from the associative array
Mixed reset (array &array) resets the array pointer
Array each (array &array) returns the key/value pairs in the array and moves the array one step forward
Mixed current (array &array) returns the current cell in the array
Mixed end (array &array) moves the pointer in the array to the last bit
Mixed next (array &array) moves the pointer in the array to the next bit
Mixed prev (array &array) moves the pointer in the array up one bit
Array array_reverse (array array [, bool preserve_keys]) returns an array of cells in reverse order
Array array_flip (array trans) reverses the key roles in the array
In addition to the above functions, you can use a loop to iterate over the elements in an array, such as
The foreach (array_expr as $value)
{the statement}
The foreach (array_expr as $key = > $value)
{the statement}
Each key/value pair is extracted until all items are obtained or some internal conditions are met
Void list (mixed varname, mixed... ) assign values in an array to variables

Determines the array size and uniqueness
Int count (mixed var [, int mode]) counts the number of properties in an array of cells or objects, and the function of the same name of sizeof
Array array_count_values (array input) counts the number of occurrences of all values in the array
Array array_unique (array array) removes duplicate values from the array

Sort an array
This is said to be the heart of the calculator problem... Ha ha... It's true...
Bool sort (array &array [, int sort_flags]) sorts the array
Bool natsort (array &array) sorts arrays using the natural sort method
Bool natcasesort (array &array) sorts arrays using a natural sort method, case-insensitive
Bool rsort (array &array [, int sort_flags]) sorts the array backwards
Bool asort (array &array [, int sort_flags]) sorts the array and keeps the index
Bool array_multisort (array ar1 [, mixed arg [, mixed... [, array...]]) sorts multiple arrays or multidimensional arrays
Bool arsort (array &array [, int sort_flags]) sorts the array in reverse order and keeps the index
Bool ksort (array &array [, int sort_flags]) sorts array key names
Bool krsort (array &array [, int sort_flags]) sorts array key names in reverse order

Merge, split, join, and decompose arrays
Array array_combine (array keys, array values) creates an array with the value of one array as its key name and the value of the other array as its value
Array array_merge (array array1 [, array array2 [, array...]]) to merge one or more arrays
Array array_merge_recursive (array array1 [, array...]) ) recursively all one or more arrays
Array array_slice (array array, int offset [, int length [, Bool preserve_keys]]) take a segment from the array and create a new array. If the offset is positive, split from the offset position of the offset array switch. If the offset is negative, split from the offset position at the end of the array
Array array_splice (array &input, int offset [, int length [, array replacement]]) removes some values from the array and replaces them with other values
Array array_intersect (array array1, array array2 [, array...] ) computes the intersection of arrays, that is, if the value that appears in the first array appears in the next several arrays, then the value is retrieved
Array array_intersect_assoc (array array1, array array2 [, array...] ) checks the intersection in the array with an index
Array array_intersect_key (array array1, array array2 [, array...] ) USES the key name to compare the intersection in the array
Array array_diff (array array1, array array2 [, array...] ) calculate the difference set of the array, that is, the value that is different from that in the first array
Array array_diff_assoc (array array1, array array2 [, array...] ) checks for differences in an array with an index
Array array_diff_key (array array1, array array2 [, array...] ) USES the key name to compare the difference set in the array

Other useful array functions
There are a lot of array functions that are not listed... A few more useful and more often, the rest of the reference manual... It's clear in the manual
Mixed array_rand (array input [, int num_req]) array randomly picks one or more keys,num specified
The bool shuffle (array &array) scrambles the array
Number array_sum (array array) computes the sum of all the values in the array, ignoring the associative array
Array array_chunk (array input, int size [, bool preserve_keys]) splits an array into several chunks

Related articles: