ange($low, $high),range($low, $high, $step);//Create an array of sequential values such as: range(1,4) is (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 decompose an array into a series of values, such as list($a,$b)=each($array)
shuffle($array), array_rand($arg, $num_req); randomly sort the array
array_reverse($input), array_reverse($input, $preserve_keys) returns the reverse sorting of the original array
sort($array); sorts the array
PHP array is an important concept, it contains a large number of functions, which is convenient for people The development of... now classifies its arrays to facilitate query and application.
First let’s talk about the definition of PHP array... PHP array contains two items, key and value. The corresponding value can be obtained through key, where key can be Numerical and related ones, such as $array[0], $array[one]...
Create arrays
The array declaration in PHP is slightly different from that in other languages, but it can still be declared as one-dimensional, two-dimensional, or three-dimensional and multi-dimensional, such as
$array[0] = 1,$array = array(1,2,3); A one-dimensional array only includes three values and is a numeric array. You can use $array[0] when referencing it. Represents 1, the index can be omitted when creating a numerical array.
Copy the code The code is as follows:
$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 as $array[4]["one"] to represent 1.
Three-dimensional and above, and so on...
If you want to create arrays in batches, you can use the following function:
array range (mixed low, mixed high [, number step] )
For example, $array = range(1,6); represents array(1,2,3,4,5,6);
$array = range(a, f); represents array(a,b,c,d,e,f);
output array
There are many functions for outputting arrays in PHP, the commonly used ones are
bool print_r (mixed expression [, bool return])
void var_dump (mixed expression [, mixed expression [, ...]] )
There are also methods like echo, print, and printf that can output a single array.
Testing arrays
Sometimes we need to determine whether a variable is an array, you can use:
bool is_array (mixed var)
Add or delete array elements
The array is not immutable after it is declared. In-depth operations may be performed by adding and deleting the array:
int array_push (array &array, mixed var [, mixed ...] ) Push one or more units to the end of the array. The length of the array increases according to the number of variables pushed onto the stack, such as array_push($array,$var)
mixed array_pop (array &array) Pop the last element of the array (pop off the stack) ), and resets the array's pointer when finished
mixed array_shift ( array &array ) returns the first element of the array.
int array_unshift ( array &array, mixed var [, mixed ...] ) inserts an or at the beginning of the array Multiple units
array array_pad (array input, int pad_size, mixed pad_value) fills the array to the specified length with values, such as array_pad($array,3,$var);
Positioning array elements
bool in_array (mixed needle, array haystack [, bool strict] ) Checks whether a certain value exists in the array
array array_keys ( array input [, mixed search_value [, bool strict]] ) Returns all the key names in the array and reorganizes them into a new array
bool array_key_exists ( mixed key, array search ) Checks if the given key exists in the array.
array array_values ( array input ) Returns all the values in the array
mixed array_search ( mixed needle, array haystack [, bool strict] ) Searches the given key in the array The value will return the key if successful.
Traverse the array
PHP provides many functions to obtain key and value
mixed key (array &array) Get the key name from the associative array
mixed reset (array &array) Reset the array pointer
array each (array &array) returns the key/value pair in the array and moves the array forward one step
mixed current (array &array) returns the current element 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 to the previous bit
array array_reverse ( array array [, bool preserve_keys] ) Returns a unit order The opposite array
array array_flip (array trans) swaps the key-value roles in the array
In addition to the above functions, you can also use loops to traverse the elements in the array, such as
foreach (array_expr as $value)
{ statement }
foreach (array_expr as $key=>$value)
{ statement }
Extract each key/value pair until all items are obtained or some internal conditions are met
void list ( mixed varname, mixed ... ) Assign the values in the array to some variables
Determine array size and uniqueness
int count (mixed var [, int mode] ) counts the number of attributes in a cell array or object in an array. 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) shift Remove duplicate values in the array
Array sorting
I heard that this is the core problem of calculators... Haha... It is also true...
bool sort (array &array [, int sort_flags]) Sort the array
bool natsort (array &array) Use Sort an array using natural sorting
bool natcasesort ( array &array ) Sort an array using natural sorting, case-insensitive
bool rsort ( array &array [, int sort_flags] ) Sort an array in reverse order
bool asort ( array &array [, int sort_flags] ) Sort arrays and maintain index relationships
bool array_multisort ( array ar1 [, mixed arg [, mixed ... [, array ...]]] ) Sort multiple arrays or multidimensional arrays
bool arsort ( array &array [, int sort_flags] ) sorts the array in reverse order and maintains the index relationship
bool ksort ( array &array [, int sort_flags] ) sorts the array by key name
bool krsort ( array &array [, int sort_flags] ) pairs Arrays are sorted in reverse order by key name
Merge, split, join and decompose arrays
array array_combine (array keys, array values) Create an array with the values of one array as its keys and the values of the other array as its values
array array_merge ( array array1 [, array array2 [, array ...]] ) Merge one or more arrays
array array_merge_recursive ( array array1 [, array ...] ) Recursively merge all one or more arrays
array array_slice ( array array, int offset [, int length [, bool preserve_keys]] ) Remove a segment from the array and create a new array. If offset is a positive number, splitting starts from the offset position from the array switch. If it is a negative number, splitting starts from the offset position of the array switch. Starts at the offset position from the end of the array, and ends at the count(input_array)-|length| position of the array switch
array array_splice (array &input, int offset [, int length [, array replacement]] ) puts some of the values in the array Remove and replace it with other values. The offset setting is the same as above
array array_intersect (array array1, array array2 [, array ...]) Calculate the intersection of arrays, that is, if the values that appear in the first array are in the next few If it appears in both arrays, take out the value
array array_intersect_assoc ( array array1, array array2 [, array ...] ) Check the intersection in the array with index
array array_intersect_key ( array array1, array array2 [, array ...] ] ) Use key names to compare intersections in arrays
array array_diff ( array array1, array array2 [, array ...] ) Calculate the difference of the array, that is, the values that are different from the first array
array array_diff_assoc ( array array1, array array2 [, array ...] ) Check the difference in the array with index
array array_diff_key ( array array1, array array2 [, array ...] ) Use the key name to compare the difference in the array
Others are more useful There are many array functions
There are many array functions that are not listed... Here are a few more useful and common ones. For the others, just refer to the manual... The manual is very clear
mixed array_rand (array input [, int num_req] ) Random in the array Take out one or more keys, num specifies the number
bool shuffle (array &array) shuffle the array
number array_sum (array array) calculate the sum of all values in the array, associative array ignores
array array_chunk (array input, int size [ , bool preserve_keys] ) splits an array into several
The above has introduced the summary of the work of the mathematics lesson preparation group and the related functions of PHP operation arrays, including the summary of the work of the mathematics lesson preparation group. I hope it will be helpful to friends who are interested in PHP tutorials.