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. Tianya will explain the most commonly used ones in detail below. .
array_change_key_case — Returns an array whose string key names 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.phpha.com', 'Blog'=>'http://blog.phpha.com');
$phpha_upper = array_change_key_case($phpha, CASE_UPPER);
$phpha_lower = array_change_key_case($phpha, CASE_LOWER); //Default case
print_r($phpha_upper);
print_r($phpha_lower);
?>
//Result:
Array
(
[HOME] => http://www.phpha.com
[BLOG] => http://blog.phpha.com
)
Array
(
[home] => http ://www.phpha.com
[blog] => http://blog.phpha.com
)
array_chunk — Split an array into multiple
array array_chunk ( array $ input , int $size [, bool $preserve_keys ] )
array_chunk() Split 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.
Set the optional parameter preserve_keys to TRUE to enable PHP to retain 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.
$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 with the value of one array as its key and the value of another array as its value
array array_combine ( array $keys , array $values )
Returns an array, Use values from the keys array as key names and values from the values array as corresponding values.
Return FALSE if the number of cells in the two arrays is different or the array is empty.
$key = array('Home', 'Blog');
$key2 = array('Home', 'Blog', 'BBS');
$phpha = array(' http://www.phpha.com', 'http://blog.phpha.com');
$phpha_combine = array_combine($key, $phpha);
$phpha_combine_wrong = array_combine($key2, $phpha);
print_r($phpha_combine);
print_r($phpha_combine_wrong);
?>
//Result:
Array
(
[Home] => http://www.phpha.com
[Blog] => http://blog.phpha.com
)
// You can see that array_combine() reported an error for the second time, pointing out that the number of members of the two arrays is not equal
Warning: Both parameters should have an equal number of elements in F :Program FilesPHPNOWhtdocsindex.php on line 31
array_count_values — Count the number of occurrences of all values in an array
array array_count_values ( array $input )
array_count_values() returns an array that uses the value in the input array as the key name and the number of times the value appears in the input array as the value.
$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 Difference of arrays
array_diff_key — Computes the difference of an array using key comparison
array_diff_ukey — Computes the difference of an array using a callback function on key comparison
array_diff_assoc — Computes the difference of an array with index checking
array_diff_uassoc — Uses a user-provided callback The function does index checking to calculate the difference of arrays
//array_diff() returns an array that includes all values in array1
//but not in any other parameter array. Note that the key names remain unchanged.
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
//Result:
Array
(
[1] => blue
)
//This function is the same as array_diff() except that the comparison is based on key names instead of values.
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array( 'green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
print_r(array_diff_key($array1, $array2));
?>
//结果:
Array
(
[red] => 2
[purple] => 4
)
//注意和 array_diff() 不同的是键名也用于比较。
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
print_r(array_diff_assoc($array1, $array2));
?>
//结果:
Array
(
[b] => brown
[c] => blue
[0] => red
)
array_fill — 用给定的值填充数组
array_fill_keys — Fill an array with values, specifying keys
array_filter — 用回调函数过滤数组中的单元
function func_check($i){
return $i > 3 ? TRUE : FALSE;
}
$array1 = array(2, 3, 5, 6);
$array2 = array(NULL, '', 'hello');
$array3 = array_filter($array1, 'func_check');
$array4 = array_filter($array2);
//函数func_check()用来判断给定的值,返回TRUE或FALSE
//返回TRUE,则$array1中的值则会返回且键名不变,否则被过滤掉
print_r($array3);
//如果没有指定回调函数,则默认过滤掉array2中为等值为FALSE的成员
//进行类型转换。因此本函数常用量顾虑掉数组中的空成员。
print_r($array4);
?>
//Result:
Array
(
[2] => 5
[3] => 6
)
Array
(
[2] => hello
)
array_flip — swap keys and values in an array
//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 — Computes the intersection of arrays
array_intersect_assoc — Computes the intersection of arrays with index checking
array_intersect_uassoc — Computes the intersection of arrays with index checking, compares the indices with a callback function
array_intersect_key — Computes the intersection of arrays using key name comparison
array_intersect_ukey — Use The callback function compares the key names to calculate the intersection of the arrays
$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
)
//Note that the difference between array_intersect_assoc() and array_intersect() is that key names are also used for comparison.
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_intersect_assoc($array1, $array2);
print_r($result);
?>
//Result:
Array
(
[a] => green
)
The above is excerpted from the PHP manual [9] – Array array function. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!