php tutorial array introduction to arrays and array functions
The array function allows you to operate on arrays.
php supports single-dimensional and multi-dimensional arrays. At the same time, a function is provided to construct an array using the database tutorial query results.
Install
array functions are an integral part of php core. No installation is required to use these functions.
php array function
php: Indicates the earliest php version that supports this function.
PHP’s array is actually an associative array, or a hash table. PHP does not need to declare the size of the array in advance, and can create the array by direct assignment. For example:
//The most traditional, use numbers as keys and assign values
$state[0]="beijing";
$state[1]="hebei";
$state[2]="tianjin";
//If the key is an increasing number, you can omit
$city[]="shanghai";
$city[]="tianjin";
$city[]="guangzhou";
//Use string as key
$capital["china"]="beijing";
$capital["japan"]="tokyo";
It is more convenient to create an array with array(). You can pass array elements to it as array parameters, or you can use the => operator to create an associative array. For example:
$p=array(1,3,5,7);
$capital=array(“china”=>”beijing”, “japan=>”tokyo”);
Array is actually a grammatical structure, not a function. Similar to array, there is also list(), which can be used to extract values from an array and assign values to multiple variables. For example:
list($s,$t)=$city;
echo $s,' ',$t;
Output result:shanghai tianjin
Note that the list method can only be used with arrays indexed by numbers.
PHP has built-in some commonly used array processing functions, please refer to the manual for details. Examples of commonly used functions are as follows, count or sizeof can get the length of an array, array_merge can merge two or more arrays, and array_push (pop) can use arrays like a stack.
Copy the code The code is as follows:
$state[0]="beijing";
$state[1]="hebei";
$state[2]="tianjin";
$city[]="shanghai";
$city[]="tianjin";
$city[]="guangzhou";
$capital["china"]="beijing";
$capital["japan"]="tokyo";
echo count($city),'
';
array_push($capital,"paris");
$newarray=array_merge($city,$capital);
foreach($newarray as $elem)
echo $elem.'
';
?>
The output result is:
3
shanghai
tianjin
guangzhou
beijing
tokyo
paris
p
Function Description php
array() creates an array. 3
array_change_key_case() returns an array whose keys are all uppercase or lowercase. 4
array_chunk() splits an array into new array chunks. 4
array_combine() creates a new array by merging two arrays. 5
array_count_values() is used to count the number of occurrences of all values in an array. 4
array_diff() returns the difference array of two arrays. 4
array_diff_assoc() compares the key name and key value and returns the difference array of the two arrays. 4
array_diff_key() compares key names and returns an array of differences between the two arrays. 5
array_diff_uassoc() calculates the difference of an array by doing an index check using a user-provided callback function. 5
array_diff_ukey() uses the callback function to compare the key names to calculate the difference of the array. 5
array_fill() fills an array with the given values. 4
array_filter() uses a callback function to filter elements in an array. 4
array_flip() swaps keys and values in an array. 4
array_intersect() calculates the intersection of arrays. 4
array_intersect_assoc() compares key names and key values and returns the intersection array of the two arrays. 4
array_intersect_key() Computes the intersection of arrays using key name comparison. 5
array_intersect_uassoc() calculates the intersection of arrays with index checking and compares the indices with a callback function. 5
array_intersect_ukey() uses a 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 keys in the array. 4
array_map() applies a callback function to the cells of the given array. 4
array_merge() Merges one or more arrays into a single array. 4
array_merge_recursive() Merges one or more arrays recursively. 4
array_multisort() Sorts multiple arrays or multidimensional arrays. 4
array_pad() pads an array with values to the specified length. 4
array_pop() pops (pops) the last element of the array. 4
array_product() calculates the product of all values in an array. 5
array_push() pushes one or more cells (elements) to the end of the array (push). 4
array_rand() randomly selects one or more elements from an array and returns it. 4
array_reduce() uses a callback function to iteratively reduce an array to a single value. 4
array_reverse() reverses the order of elements in the original array, creates a new array and returns it. 4
array_search() searches the array for a given value and returns the corresponding key if successful. 4
array_shift() deletes the first element in the array and returns the value of the deleted element. 4
array_slice() removes a segment of value from the array based on conditions and returns it. 4
array_splice() removes a portion of an array and replaces it with another value. 4
array_sum() calculates the sum of all values in an array. 4
array_udiff() uses a callback function to compare data to calculate the difference of arrays. 5
array_udiff_assoc() calculates the difference of an array with index checking and compares the data using a callback function. 5
array_udiff_uassoc() calculates the difference set of the array with index checking, using a callback function to compare the data and index. 5
array_uintersect() calculates the intersection of arrays and uses callback functions to compare data. 5
array_uintersect_assoc() calculates the intersection of arrays with index checking and compares data using callback functions. 5
array_uintersect_uassoc() calculates the intersection of arrays with index checking, using a callback function to compare the data and index. 5
array_unique() removes duplicate values from an 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 a user function to each member of the array. 3
array_walk_recursive() recursively applies a user function to each member of an array. 5
arsort() sorts an array in reverse order and maintains index relationships. 3
asort() sorts an array and maintains index relationships. 3
compact() creates an array including variable names and their values. 4
count() counts the number of elements in an array or the number of attributes in an 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 forward one step. 3
end() sets the array's internal pointer to the last element. 3
extract() imports variables from an array into the current symbol table. 3
in_array() checks whether the specified value exists in the array. 4
key() gets the key name from an associative array. 3
krsort() sorts the array in reverse order by key name. 3
ksort() sorts the array by key name. 3
list() assigns the values in an array to some variables. 3
natcasesort() sorts an array in a case-insensitive manner using the "natural sort" algorithm. 4
natsort() sorts an array using the "natural sorting" algorithm. 4
next() moves the internal pointer in the array forward one position. 3
pos() Alias for current(). 3
prev() rolls back the array's internal pointer one bit. 3
range() creates an array containing elements in the specified range. 3
reset() sets the array's internal pointer to the first element. 3
rsort() sorts an array in reverse order. 3
shuffle() rearranges the elements in the array in random order. 3
sizeof() Alias for count(). 3
sort() sorts an array. 3
uasort() sorts the values in an array using a user-defined comparison function and maintains index association. 3
uksort() sorts the keys in an array using a user-defined comparison function. 3
usort() sorts the values in an array using a user-defined comparison function. 3
php array constant
php: Indicates the earliest php version that supports this constant.
Constant Description php
case_lower is used in array_change_key_case() to convert array key names to lowercase letters.
case_upper is used in array_change_key_case() to convert array key names to uppercase letters.
sort_asc is used in the array_multisort() function to sort in ascending order.
sort_desc is used in the array_multisort() function to sort items in descending order.
sort_regular is used for general comparison of objects.
sort_numeric is used to perform numerical comparisons of objects.
sort_string is used for string comparison of objects.
sort_locale_string Performs a string comparison of objects based on the current locale. 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