php中的数组函数学习记录1
1、返回字符串键名全为小写或大写的数组。array_change_key_case
用法:array_change_key_case($input (array),CASE_UPPER|CASE_LOWER),其中CASE_LOWER是返回小写(默认),CASE_UPPER返回大写
例子:
$input_array = array("FirSt" => 1, "SecOnd" => 4);
var_dump(array_change_key_case($input_array, CASE_UPPER));
var_dump(array_change_key_case($input_array));
结果:array(2) { ["FIRST"]=> int(1) ["SECOND"]=> int(4) }
array(2) { ["first"]=> int(1) ["second"]=> int(4) }
2、将一个数组分为多个数组。array_chunk
用法:array_chunk($input(array),$size(int),TRUE|FALSE),其中$size是每一个新的数组的个数,TRUE会保留原来的键名,FALSE会重新从数字索引开始(默认)。
$input_array = array('a'=>'python',"b"=>"java","c"=>"php","d"=>"mysql","e"=>"javascript","f"=>"redis","g"=>"R","h"=>"mongodb");
var_dump(array_chunk($input_array, 2));
var_dump(array_chunk($input_array, 2, true));
结果:array(4) { [0]=> array(2) { [0]=> string(6) "python" [1]=> string(4) "java" } [1]=> array(2) { [0]=> string(3) "php" [1]=> string(5) "mysql" } [2]=> array(2) { [0]=> string(10) "javascript" [1]=> string(5) "redis" } [3]=> array(2) { [0]=> string(1) "R" [1]=> string(7) "mongodb" } }
array(4) { [0]=> array(2) { ["a"]=> string(6) "python" ["b"]=> string(4) "java" } [1]=> array(2) { ["c"]=> string(3) "php" ["d"]=> string(5) "mysql" } [2]=> array(2) { ["e"]=> string(10) "javascript" ["f"]=> string(5) "redis" } [3]=> array(2) { ["g"]=> string(1) "R" ["h"]=> string(7) "mongodb" } }
3、创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 array_combine
用法: array_combine($input1(array),$input2(array)).其中$input1中数组为键名,$input2中数组为键值,如果长度不一样则返回FALSE;
$a=array("a","b","c");
$b=array("php","python","mysql");
$c = array_combine($a, $b);
var_dump($c);
$e=array("a","b");
$f=array_combine($e,$b);
var_dump($f);
结果:
array(3) { ["a"]=> string(3) "php" ["b"]=> string(6) "python" ["c"]=> string(5) "mysql" }
Warning: array_combine() [function.array-combine]: Both parameters should have an equal number of elements in D:\WWW\array\arraytest.php on line 8
bool(false)
4、统计数组中所有的值出现的次数 array_count_values
用法:array_count_values ( $input(array) )
$input_array = array('a'=>'python',"b"=>"java","c"=>"php","d"=>"mysql","e"=>"javascript","f"=>"redis","g"=>"R","h"=>"mongodb");
$array = array(1, "hello", 1, "world", "hello");
var_dump(array_count_values ($array));
var_dump(array_count_values ($input_array));
结果:
array(3) { [1]=> int(2) ["hello"]=> int(2) ["world"]=> int(1) }
array(8) { ["python"]=> int(1) ["java"]=> int(1) ["php"]=> int(1) ["mysql"]=> int(1) ["javascript"]=> int(1) ["redis"]=> int(1) ["R"]=> int(1) ["mongodb"]=> int(1) }
5、计算数组的差集-array_diff
用法:array_diff($input1(array),$input2(array),$input(array)....);返回在$input中但不在其他数组中的值
$input_array = array('python',"b"=>"java","c"=>"php","d"=>"mysql","e"=>"javascript","f"=>"redis","R","h"=>"mongodb");
$array = array("javascript","f"=>"redis","g"=>"R","h"=>"mongodb");
var_dump(array_diff($input_array,$array));
结果:array(4) { [0]=> string(6) "python" ["b"]=> string(4) "java" ["c"]=> string(3) "php" ["d"]=> string(5) "mysql" }
6、用回调函数对键名比较计算数组的差集-array_diff_ukey
用法和array_diff一样,只是array_diff是根据键值,array_diff_ukey只是根据键名,不过要通过一个回调函数进行比较,虽然说是用户可以自定义,不过看网上其他的说法,还是手册,貌似那个回调函数都一个样子,有明白的可以给我指点一下,直接上例子,先能用。
function key_compare_func($key1, $key2){
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_diff_ukey($array1, $array2, 'key_compare_func'));
结果:array(2) { ["red"]=> int(2) ["purple"]=> int(4) }(都是求差集,所谓的回调函数也就这样。。。)
7、用用户提供的回调函数做索引检查来计算数组的差集-----array_diff_uassoc
用法:上面用法一样,只是在键值的基础上,键名也参与了比较
function key_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$array3=array("a" => "green", "red", "b" => "brown", "c" => "blue");
$array4=array("a" => "green", "red", "yellow");
var_dump(array_diff_uassoc($array1, $array2, "key_compare_func"));
var_dump(array_diff_uassoc($array3, $array4, "key_compare_func"));
结果:array(3) { ["b"]=> string(5) "brown" ["c"]=> string(4) "blue" [0]=> string(3) "red" }
array(2) { ["b"]=> string(5) "brown" ["c"]=> string(4) "blue" }
8、使用键名比较计算数组的差集,array_diff_key
用法和array_diff一样,这个是通过键名来求差集,更重要的是没有什么回调函数
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_diff_key($array1, $array2));
结果:array(2) { ["red"]=> int(2) ["purple"]=> int(4) }
9、带索引检查计算数组的差集 array_diff_assoc
用法和array_diff,array_diff_key一样,在比较时候加入了索引
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "red", "yellow");
$array3 = array ("a" => "green", "yellow", "red");
var_dump(array_diff_assoc($array1, $array2));
var_dump(array_diff_assoc($array1, $array3));
结果:array(2) { ["b"]=> string(5) "brown" ["c"]=> string(4) "blue" }
array(3) { ["b"]=> string(5) "brown" ["c"]=> string(4) "blue" [0]=> string(3) "red" }
10、用给定的值填充数组--array_fill
用法:array_fill($start_index(int),$num(int),$value)用 value 参数的值将一个数组填充 num 个条目,键名start_index 参数指定的开始。注意 num 必须是一个大于零的数值
var_dump(array_fill(5, 6, 'banana'));
结果:array(4) { [5]=> string(6) "banana" [6]=> string(6) "banana" [7]=> string(6) "banana" [8]=> string(6) "banana" }
11、使用指定的键和值填充数组--array_fill_keys
array_fill_keys($input(array),$value(string))使用 value 参数的值作为值,使用 keys 数组的值作为键来填充一个数组。
$keys = array('foo', 5, 10, 'bar'=>'1');
var_dump(array_fill_keys($keys, 'banana'));
结果:array(4) { ["foo"]=> string(6) "banana" [5]=> string(6) "banana" [10]=> string(6) "banana" [1]=> string(6) "banana" }
12、用回调函数过滤数组中的单元 array_filter
array_filter($input(array),$callback(function)),$input中的值会传给回调函数,通过回调函数进行判断,再返回给结果数组中。
function odd($var){
return($var % 2 == 1);
}
function even($var){
return($var % 2 == 0);
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);
var_dump(array_filter($array1, "odd"));
var_dump(array_filter($array2, "even"));
结果:array(3) { ["a"]=> int(1) ["c"]=> int(3) ["e"]=> int(5) }
array(4) { [0]=> int(6) [2]=> int(8) [4]=> int(10) [6]=> int(12) }
13、交换数组中的键和值-array_flip($input(array))貌似这个主要用的是关联数组
用法:输入一个数组,交换键名很键值的位置。键名需要是合法的如:string或者int型,
如果值的类型不对将发出一个警告,并且有问题的键/值对将不会反转。如果同一个值出现了多次,则最后一个键名将作为它的值,所有其它的都丢失了。array_flip() 如果失败返回 FALSE。
$array = array("javascript","f"=>"redis","g"=>"R","h"=>"mongodb");
var_dump(array_flip($array));
结果:array(4) { ["javascript"]=> int(0) ["redis"]=> string(1) "f" ["R"]=> string(1) "g" ["mongodb"]=> string(1) "h" }
14、计算数组的交集-array_intersect
用法:array_intersect(array1,array2...),就是计算多个数组的交集,再以数组返回回来
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
var_dump(array_intersect($array1, $array2));
结果:array(2) { ["a"]=> string(5) "green" [0]=> string(3) "red" }
15、用回调函数比较键名来计算数组的交集-array_intersect_ukey
array_intersect_ukey(array1,array2,....,function)通过回调函数来计算数组的交集
此比较是通过用户提供的回调函数来进行的。如果认为第一个参数小于,等于,或大于第二个参数时必须分别返回一个小于零,等于零,或大于零的整数。
function key_compare_func($key1, $key2){
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func'));
结果:array(2) { ["blue"]=> int(1) ["green"]=> int(3) }
16、带索引检查计算数组的交集,用回调函数比较索引-array_intersect_uassoc
array_intersect_uassoc(array1,array2,....,function)返回的数组中包含了所有在 array1 中也同时出现在所有其它参数数组中的值
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
print_r(array_intersect_uassoc($array1, $array2, "strcasecmp"));
结果:Array ( [b] => brown )
17、使用键名比较计算数组的交集--array_intersect_key
用法:array_intersect_key(array1,array2,....)返回的数组包含了所有出现在array1 中并同时出现在所有其它参数数组中的键名的值。
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_key($array1, $array2));
结果:array(2) { ["blue"]=> int(1) ["green"]=> int(3) }
18、带索引检查计算数组的交集-array_intersect_assoc
用法:和array_diff_assoc一样,只不过不同的是一个求交集,一个求并集
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$array3 = array("a" => "green", "red", "b" => "brown", "c" => "blue");
$array4 = array("a" => "green", "red", "yellow");
var_dump(array_intersect_assoc($array1, $array2));
var_dump(array_intersect_assoc($array3, $array4));
结果:
array(1) { ["a"]=> string(5) "green" } array(2) { ["a"]=> string(5) "green" [0]=> string(3) "red" }