array函数举例
1.array_merge($arr1,$arr2,$arr3,…)用于合并多个数组
$arr1 = [2,3,4,5,6,7,8];
$arr2 = ['a','b','c','d','e'];
$arr3 = ['hi','bob'];
printf("<pre>%s</pre>", print_r(array_merge($arr1,$arr2,$arr3), true));
# 结果是:
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
[6] => 8
[7] => a
[8] => b
[9] => c
[10] => d
[11] => e
[12] => hi
[13] => bob
)
2.array_chunk($arr, $size, $bool)用于拆分数组。第一个参数是拆分的数组,第二个拆分完后子数组元素的个数,第三个true的话就保留键不变,否则重置键。
$arr1 = [2,3,4,5,6,7,8];
printf("<pre>%s</pre>", print_r(array_chunk($arr1, 3,true),true));
结果是:
Array
(
[0] => Array
(
[0] => 2
[1] => 3
[2] => 4
)
[1] => Array
(
[3] => 5
[4] => 6
[5] => 7
)
[2] => Array
(
[6] => 8
)
)
3.range($start, $end, $step)用于生成数组。
printf("<pre>%s</pre>", print_r(range(1,11,3),true));
# 结果是:
Array
(
[0] => 1
[1] => 4
[2] => 7
[3] => 10
)
array_fill($index,$number,$value)第一个参数是新数组其实索引,第二个是插入新数组的元素个数,第三个参数是插入新数组的值。
printf("<pre>%s</pre>", print_r(array_fill(4,4,[1,2]),true));
#结果是:
Array
(
[4] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[5] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[6] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[7] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
5.array_fill_keys($key,$value)用于生成指定键的数组
printf("<pre>%s</pre>", print_r(array_fill_keys([3,4,5,6,7],4),true));
结果是:
Array
(
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
)
6.array_rand($arr, $number)用于从数组里随机取出$number 个元素,如果$number超出元素个数的话就会报错
$arr1 = [2,3,4,5,6,7,8];
printf("<pre>%s</pre>", print_r(array_rand($arr1,3),true));
# 结果是:
Array
(
[0] => 3
[1] => 4
[2] => 6
)
7.array_column($arr,$column,$index_key),用于从二维数组或者更加高维的数组中提取某一列生成新的数组。$arr表示原数组,$column表示从哪一列提取,如果没有的话就返回null。$index_key表示生成数组的key。如果是数组的话就从0开始往下排。
$arr = [];
$arr[] = ['a','b','c','d'];
$arr[] = ['h',[3,4,5],'f','e'];
$arr[] = [1,2,3,4,5,6];
printf("<pre>%s</pre>", print_r(array_column($arr,1,1),true));
# 结果是:
Array
(
[b] => b
[0] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
[2] => 2
)
8.array_replace($arr1,$arr2,$arr3,…)替换值。第二个数组替换第一个数组对应键的值。后面的数组同样如此替换$arr1的值。
$arr1 = [2,3,4,5,6,7,8];
$arr2 = ['a','b','c','d','e'];
printf("<pre>%s</pre>", print_r(array_replace($arr1,$arr2),true));
# 结果是:
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => 7
[6] => 8
)
9.array_intersect($arr1,$arr2,$arr3,…)求出$arr1,$arr2,$arr3共有的元素,不比较键。array_intersect_assoc()既比较键名也比较键值。
$arr1 = [2,3,4,5,6,7,8];
$arr2 = [1,3,4,5];
printf("<pre>%s</pre>", print_r(array_intersect($arr1,$arr2),true));
# 结果是:
Array
(
[1] => 3
[2] => 4
[3] => 5
)
10.array_diff($arr1,$arr2,$arr3,…)返回只存在于$arr1,而不存在于其他数组的元素值。不比较键名,键名不会发生改变,只比较键值。array_diff_assoc().
$arr1 = [2,3,4,5,6,7,8];
$arr2 = [1,3,4,5];
printf("<pre>%s</pre>", print_r(array_diff($arr1,$arr2),true));
# 结果是:
Array
(
[0] => 2
[4] => 6
[5] => 7
[6] => 8
)
回调函数
1.array_reduce($array, $function, [$init])。$array表示数组。$function代表可以回调的函数。该函数至多两个参数。$init表示初始值。如果$init不是null,那么会作为数组的第一个值,或当数组是空数组时,作为array_reduce()的返回值。
$arr1 = [2,3,4,5,6,7,8];
print_r(array_reduce($arr1, function($v1, $v2){
return $v1 - $v2;
}));
# 结果是: -35(0-2-3-4-5-6-7-8)
如果我们给一个初始值:
$arr1 = [2,3,4,5,6,7,8];
print_r(array_reduce($arr1, function($v1, $v2){
return $v1 - $v2;
},6));
# 结果是: -29 (6-2-3-4-5-6-7-8)
或者改为字符串拼接
$arr1 = [2,3,4,5,6,7,8];
print_r(array_reduce($arr1, function($v1, $v2){
return $v1.$v2;
},6));
# 结果是:62345678
array_filter($array, $function, [$flag]).第一个参数表示数组,第二个参数表示回调函数,当回调函数返回结果bool是真时,将真的$array的键和值放入返回数组中去。$flag有两个值。分别是:ARRAY_FILTER_USE_BOTH(1),ARRAY_FILTER_USE_KEY(2).如果是1,将数组里的键名和键值都传入回调函数中去。2就只传键名。
$arr1 = [1,2,3,4,0];
print_r(array_filter($arr1, function($key,$value){
return $key * $value;
},ARRAY_FILTER_USE_BOTH));
# 结果是:
Array ( [1] => 2 [2] => 3 [3] => 4 )
$arr1 = [1,2,3,4,0];
print_r(array_filter($arr1, function($key){
return $key;
},ARRAY_FILTER_USE_KEY));
# 结果是:
Array ( [1] => 2 [2] => 3 [3] => 4 [4] => 0 )
3.array_map($function, $arr1,$arr2,…)$function 表示可回调的函数,其参数个数与后面传入的数组个数有关。多少个数组就至多可以传入多少个参数。$arr的键值作为参数调入回调函数中。
$arr1 = [1,2,3,4,0];
$arr2 = [2,3,4,5,6];
print_r(array_map(function($value1,$value2){
return $value1 * $value2;
},$arr1,$arr2));
# 结果是:
Array ( [0] => 2 [1] => 6 [2] => 12 [3] => 20 [4] => 0 )
array_walk($array, $function, [$userdata]) $array表示传入的数组。$function有三个参数,第一、二个分别是传入数组的键值和键名,第三个是$userdata,可有可无。返回结果是bool值。如果函数执行成功返回bool真值。
$arr2 = [2,3,4,5,6];
echo array_walk($arr2,function($value, $key){
return "[$key]=>[$value]<br />";
});
# 结果是: 1
$arr2 = [2,3,4,5,6];
echo array_walk($arr2,function($value, $key, $user){
return $value * $key * $user;
}, 0);
# 结果是: 1
$arr2 = [2,3,4,5,6];
array_walk($arr2,function($value, $key, $user){
echo $value * $key * $user, "<br />";
}, 0);
# 结果是:
0
0
0
0
0
array_slice()和array_splice()很相似。一个用于从数组中取值,一个用于对数组进行增删修改。
array_slice($array, $offset, $length, $preserve_key) 第一个表示数组,第二个参数表示从第几个元素开始切片,可以是负值,负值表示倒数几个元素。第三个参数表示切片长度,正值表示长度,负值表示到倒数几个元素(不包括该元素)。第四个参数表示保持键名不变。
$arr1 = [1,2,3,4,5];
print_r(array_slice($arr1, 2,2,true));
# 结果是:Array ( [2] => 3 [3] => 4 )
$arr1 = [1,2,3,4,5];
print_r(array_slice($arr1, -5,-1,true))
# 结果是:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
array_splice($array, $offset, $length, $replace_arr).前三个参数与array_slice()意义一样。第四个参数表示替换的数组,如果数组只有一个值,可以用字符串代替。
$arr1 = [1,2,3,4,5];
print_r(array_splice($arr1,0,2,[1,23,4,5,7,8]));
echo "<hr />";
print_r($arr1);
# 结果是:
[0] => 1 [1] => 2 )
Array ( [0] => 1 [1] => 23 [2] => 4 [3] => 5 [4] => 7 [5] => 8 [6] => 3 [7] => 4 [8] => 5 )
[0] => 1 [1] => 2 )
Array ( [0] => 1 [1] => 23 [2] => 4 [3] => 5 [4] => 7 [5] => 8 [6] => 3 [7] => 4 [8] => 5 )
# 结果是:
)
Array ( [0] => 1 [1] => 1 [2] => 23 [3] => 4 [4] => 5 [5] => 7 [6] => 8 [7] => 2 [8] => 3 [9] => 4 [10] => 5 )
$arr1 = [1,2,3,4,5];
print_r(array_splice($arr1,-3,0,'bob'));
echo "<hr />";
print_r($arr1);
# 结果是:
Array ( )
Array ( [0] => 1 [1] => 2 [2] => bob [3] => 3 [4] => 4 [5] => 5 )