Php Tutorial array Overview and array functions
The array function allows you to operate on arrays.
Php supports single-dimension and multi-dimensional arrays. At the same time, the database query results are provided to construct an array function.
Install
Array functions are the core component of php. These functions can be used without installation.
Php array functions
Php: indicates the earliest php version that supports this function.
The php array is actually an associated array, or a hash table. Php does not need to declare the array size in advance. You can create an array by directly assigning values. For example:
// The most traditional method: Use numbers as keys and assign values.
$ State [0] = "beijing ";
$ State [1] = "hebei ";
$ State [2] = "tianjin ";
// If the key is an incremental number, it can be omitted
$ City [] = "shanghai ";
$ City [] = "tianjin ";
$ City [] = "guangzhou ";
// Use a string as the key
$ Capital ["china"] = "beijing ";
$ Capital ["japan"] = "tokyo ";
It is more convenient to use array () to create an array. You can use an array element as an array parameter and use the => operator to create an associated array. For example:
$ P = array (1, 3, 5, 7 );
$ Capital = array ("china" => "beijing", "japan =>" tokyo ");
Array is actually a syntax structure, not a function. Similar to array, there is also a list (), which can be used to extract values from the array and assign values to multiple variables. For example:
List ($ s, $ t) = $ city;
Echo $ s, '', $ t;
Output result: shanghai tianjin
Note: The list method can only be used for arrays indexed by numbers.
Php has built-in some common array processing functions. For details, refer to the manual. Examples of common functions are as follows. count or sizeof can get the length of the array. array_merge can combine two or more arrays. array_push (pop) can use arrays like stacks.
Copy the code as follows:
<? Php
$ State [0] = "beijing ";
$ State [1] = "hebei ";
$ State [2] = "tianjin ";
$ City [] = "shanghai ";
$ City [] = "tianjin ";
$ City [] = "guangzhou ";
$ Capital ["china"] = "beijing ";
$ Capital ["japan"] = "tokyo ";
Echo count ($ city), '<br/> ';
Array_push ($ capital, "paris ");
$ Newarray = array_merge ($ city, $ capital );
Foreach ($ newarray as $ elem)
Echo $ elem. '<br/> ';
?>
Output result:
3
Shanghai
Tianjin
Guangzhou
Beijing
Tokyo
Paris
P
Function description php
Array () to create an array. 3
Array_change_key_case () returns an array with uppercase or lowercase keys. 4
Array_chunk () splits an array into new array blocks. 4
Array_combine () creates a new array by combining two arrays. 5
Array_count_values () is used to count the number of occurrences of all values in the array. 4
Array_diff () returns the number of difference sets of two arrays. 4
Array_diff_assoc () compares the key name and key value, and returns the number of difference sets of the two arrays. 4
Array_diff_key () compares the key names and returns the number of difference sets of the two arrays. 5
Array_diff_uassoc () performs an index check using the callback function provided by the user to calculate the array's difference set. 5
Array_diff_ukey () uses the callback function to compare the differences of the calculated array with the key name. 5
Array_fill () fills the array with the given value. 4
Array_filter () uses the callback function to filter elements in the array. 4
Array_flip () exchanges the keys and values in the array. 4
Array_intersect () calculates the intersection of arrays. 4
Array_intersect_assoc () compares the key name and key value, and returns the intersection array of the two arrays. 4
Array_intersect_key () uses the key name to compare and calculate the intersection of arrays. 5
Array_intersect_uassoc () carries an index check to calculate the intersection of arrays and compares indexes with callback functions. 5
Array_intersect_ukey () uses the callback function to compare key names to calculate the intersection of arrays. 5
Array_key_exists () checks whether the given key name or index exists in the array. 4
Array_keys () returns all the key names in the array. 4
Array_map () applies the callback function to the units of the given array. 4
Array_merge () combines one or more numbers into an array. 4
Array_merge_recursive () recursively merges one or more arrays. 4
Array_multisort () sorts multiple arrays or multi-dimensional arrays. 4
Array_pad () fills the array with a value to the specified length. 4
Array_pop () pops up the last unit of the array (output stack ). 4
Array_product () calculates the product of all values in the array. 5
Array_push () pushes one or more units (elements) to the end of the array (inbound stack ). 4
Array_rand () randomly selects one or more elements from the array and returns them. 4
Array_reduce () iterates the array into a single value using the callback function. 4
Array_reverse () switches the elements in the original array sequentially, creates a new array, and returns the result. 4
Array_search () searches for a given value in the array. If the value is successful, the corresponding key name is returned. 4
Array_shift () deletes the first element in the array and returns the value of the deleted element. 4
Array_slice () extracts a value from the array based on the conditions and returns the result. 4
Array_splice () removes a part of the array and replaces it with other values. 4
Array_sum () calculates the sum of all values in the array. 4
Array_udiff () compares data with the callback function to calculate the difference set of the array. 5
Array_udiff_assoc () carries the index check to calculate the difference set of the array, and compares the data with the callback function. 5
Array_udiff_uassoc () carries the index check to calculate the difference set of the array, and compares data and indexes with the callback function. 5
Array_uintersect () calculates the intersection of arrays and compares the data with the callback function. 5
Array_uintersect_assoc () includes an index check to calculate the intersection of arrays and compares data with a callback function. 5
Array_uintersect_uassoc () includes an index check to calculate the intersection of arrays and compares data and indexes with a callback function. 5
Array_unique () deletes repeated values in the array. 4
Array_unshift () inserts one or more elements at the beginning of the array. 4
Array_values () returns all values in the array. 4
Array_walk () applies user functions to each member in the array. 3
Array_walk_recursive () recursively applies user functions to each member in the array. 5
Arsort () sorts arrays in reverse order and maintains the index relationship. 3
Asort () sorts arrays and maintains the index relationship. 3
Compact () creates an array, including the variable names and their values. 4
Count () calculates the number of elements in the array or the number of attributes in the object. 3
Current () returns the current element in the array. 3
Each () returns the current key/value pair in the array and moves the array pointer one step forward. 3
End () points the internal pointer of the array to the last element. 3
Extract () imports variables from the array to the current symbol table. 3
In_array () check whether the specified value exists in the array. 4
Key () gets the key name from the associated array. 3
Krsort () sorts arrays in reverse order by key name. 3
Ksort () sorts arrays by key names. 3
List () assigns values in the array to some variables. 3
Natcasesort () uses the "natural sorting" algorithm to sort arrays by case-insensitive letters. 4
Natsort () uses the "natural sorting" algorithm to sort arrays. 4
Next () moves the internal pointer in the array one by one. 3
The alias of pos () current. 3
Prev () returns the internal pointer of the array to one position. 3
Range () creates an array containing elements of a specified range. 3
Reset () points the internal pointer of the array to the first element. 3
Rsort () sorts arrays in reverse order. 3
Shuffle () sorts the elements in the array in random order. 3
The alias of sizeof () count. 3
Sort () sorts arrays. 3
Uasort () uses a user-defined comparison function to sort the values in the array and maintain the index Association. 3
Uksort () uses a user-defined comparison function to sort the key names in the array. 3
Usort () uses a user-defined comparison function to sort the values in the array. 3
Php array constant
Php: indicates the earliest php version that supports this constant.
Constant description php
Case_lower is used to convert the array key name into lowercase letters in array_change_key_case.
Case_upper is used to convert the array key name into uppercase letters in array_change_key_case.
Sort_asc is used in the array_multisort () function to sort it in ascending order.
Sort_desc is used in the array_multisort () function to sort it in descending order.
Sort_regular is used to compare objects.
Sort_numeric is used to compare the value of an object.
Sort_string is used to compare strings of objects.
Sort_locale_string compares strings of objects based on the current region. 4
Count_normal
Count_recursive
Extr_overwrite
Extr_skip
Extr_prefix_same
Extr_prefix_all
Extr_prefix_invalid
Extr_prefix_if_exists
Extr_if_exists
Extr_refs