Array is a special data type in php. At the same time, php also provides us with a large number of array operation functions, so that we can feel stress-free when operating arrays. Below I summarize the introductory notes of php array array. Share it with you.
Introduction: This article introduces the system functions used to perform various operations on arrays in the PHP manual. It can be said that arrays play an important role in PHP, so there are many functions. Below, Tianya lists the most commonly used ones. Detailed description.
array_change_key_case — Returns an array whose string keys are all lowercase or uppercase
array array_change_key_case ( array $input [, int $case ] )
$case can be CASE_UPPER or CASE_LOWER (default)
代码如下 | 复制代码 |
$phpha = array('Home'=>'http://www.bKjia.c0m', 'Blog'=>'http://www.bKjia.c0m'); $phpha_upper = array_change_key_case($phpha, CASE_UPPER); $phpha_lower = array_change_key_case($phpha, CASE_LOWER); //默认情况 print_r($phpha_upper); print_r($phpha_lower); ?> //结果: Array ( [HOME] => http://www.bKjia.c0m [BLOG] => http://www.bKjia.c0m ) Array ( [home] => http://www.bKjia.c0m [blog] => http://www.bKjia.c0m ) |
array_chunk — Split an array into multiple
array array_chunk ( array $input , int $size [, bool $preserve_keys ] )
array_chunk() splits an array into multiple arrays, where the number of cells in each array is determined by size. The last array may have a few fewer elements. The resulting array is a cell in a multidimensional array, with indexes starting from zero.
Setting the optional preserve_keys parameter to TRUE causes PHP to preserve the original key names in the input array. If you specify FALSE, each result array will be indexed with a new number starting from zero. The default value is FALSE.
The code is as follows | Copy code |
代码如下 | 复制代码 |
$input_array = array('a', 'b', 'c', 'd', 'e'); print_r(array_chunk($input_array, 2)); print_r(array_chunk($input_array, 2, TRUE)); ?> //结果: Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) ) Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [2] => c [3] => d ) [2] => Array ( [4] => e ) ) |
array_combine — Create an array using the values of one array as its keys and the values of another array as its values
array array_combine ( array $keys , array $values )
Returns an array with values from the keys array as key names and values from the values array as corresponding values.
Returns FALSE if the two arrays have different numbers of elements or if the arrays are empty.
The code is as follows | Copy code |
代码如下 | 复制代码 |
$key = array('Home', 'Blog'); $key2 = array('Home', 'Blog', 'BBS'); $phpha = array('http://www.bKjia.c0m', 'http://www.bKjia.c0m'); $phpha_combine = array_combine($key, $phpha); $phpha_combine_wrong = array_combine($key2, $phpha); print_r($phpha_combine); print_r($phpha_combine_wrong); ?> //结果: Array ( [Home] => http://www.bKjia.c0m [Blog] => http://www.bKjia.c0m ) |
$key2 = array('Home', 'Blog', 'BBS');
$phpha = array('http://www.bKjia.c0m', 'http://www.bKjia.c0m');
$phpha_combine_wrong = array_combine($key2, $phpha);
Print_r($phpha_combine);Print_r($phpha_combine_wrong);
?>//Result:
Array(
[Home] => http://www.bKjia.c0m代码如下 | 复制代码 |
$phpha = array('hello', 'world', 'tianya', 'hello', 'world'); $phpha_result = array_count_values($phpha); print_r($phpha_result); ?> //结果: Array ( [hello] => 2 [world] => 2 [tianya] => 1 ) |
The code is as follows | Copy code |
$phpha = array('hello', 'world', 'tianya', 'hello', 'world');<🎜> $phpha_result = array_count_values($phpha);<🎜> Print_r($phpha_result);<🎜> ?> //Result: Array ( [hello] => 2 [world] => 2 [tianya] => 1 ) |
array_diff — Calculate the difference of arrays
array_diff_key — Compute the difference of an array using key name comparison
array_diff_ukey — Use callback function to compare key names to calculate the difference of array
array_diff_assoc — Compute the difference of an array with index checking
array_diff_uassoc — Compute the difference of an array using a user-supplied callback function with index checking
The code is as follows | Copy code | ||||
//array_diff() returns an array, the The array contains everything in array1
|
array_fill — fills an array with the given values
array_fill_keys — Fill an array with values, specifying keys
array_filter — Use a callback function to filter elements in an array
The code is as follows | Copy code | ||||||||
} $array1 = array(2, 3, 5, 6);
|
The code is as follows | Copy code |
//If the same value appears multiple times , the last key name will be used as its value, and all others are lost. $trans = array("a" => 1, "b" => 1, "c" => 2); $trans = array_flip($trans); Print_r($trans); ?> //Result: Array ( [1] => b [2] => c ) |
array_intersect — Calculate the intersection of arrays
array_intersect_assoc — Compute the intersection of arrays with index checking
array_intersect_uassoc — Compute the intersection of arrays with index checking, using a callback function to compare the indices
array_intersect_key — Compute the intersection of arrays using key name comparison
array_intersect_ukey — Compute the intersection of arrays using callback functions to compare keys
The code is as follows | Copy code |
代码如下 | 复制代码 |
$array1 = array("a" => "green", "red", "blue"); $array2 = array("b" => "green", "yellow", "red"); $result = array_intersect($array1, $array2); print_r($result); ?> //结果: Array ( [a] => green [0] => red ) //注意array_intersect_assoc()和array_intersect()不同的是键名也用于比较。 $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "green", "yellow", "red"); $result = array_intersect_assoc($array1, $array2); print_r($result); ?> //结果: Array ( [a] => green ) |
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);Print_r($result);
?> //Result: Array