数组函数
创建数组
- array():生成一个数组
数组值或者键=>值一个数组型变量
-
array_combine():生成一个数组,用一个数组的值作为键名,用一个数组的值作为值
$a = ['a', 'b', 'c', 'd']; $b = ['cat', 'dog', 'cow', 'horse']; print_r(array_combine($a, $b)); /** * Array ( [a] => cat [b] => dog [c] => cow [d] => horse ) */
-
range():创建并返回一个包含指定范围的元素的数组
$number = range(0,50,10); print_r ($number); /** * Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 ) */
-
compact():创建一个由参数所带变量组成的数组
$firstname = "Peter"; $lastname = "Griffin"; $age = "38"; $result = compact("firstname", "lastname","age"); print_r($result); /** * Array ( [firstname] => Peter [lastname] => Griffin [age] => 38 ) */
变量或数组
-
array_fill():用给定的填充(值生成)数组
$a=array_fill(2,3,"Dog"); print_r($a); /** * Array ( [2] => Dog [3] => Dog [4] => Dog ) */
2是键,3是重复的次数,"dog"是填充的数据
数组合并和拆分
-
array_chunk():把一个数组分割为新的数组块
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow"); print_r(array_chunk($a,2)); /** * Array ( [0] => Array ( [0] => Cat [1] => Dog ) [1] => Array ( [0] => Horse [1] => Cow ) ) */
$a是数组,2表示分割的数组单个含有几个数组
-
array_merge():把两个或者多个数组合并成一个数组
$a1=array("a"=>"Horse","b"=>"Dog"); $a2=array("c"=>"Cow","b"=>"Cat"); print_r(array_merge($a1,$a2)); /** * Array ( [a] => Horse [b] => Cat [c] => Cow ) */
输入:两个数组
输出:返回完成后的数组
** 未完 **