参考地址:http://www.w3school.com.cn/php/php_ref_array.asp
一、数组操作的基本函数
A.数组的键名和值
1.array_values(array): 返回数组的所有值(非键名)
例:
$a=array("Name"=>"Bill","Age"=>"60","Country"=>"USA");
print_r(array_values($a));
结果:
Array (
[0] => Bill
[1] => 60
[2] => USA
)
2.array_keys(array,value,strict):返回包含数组中所有键名的一个新数组
例:
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a));
结果:
Array (
[0] => Volvo
[1] => BMW
[2] => Toyota
)
3.array_flip(array):数组中的值与键名互换(如果有重复前面的会被后面的覆盖)
例:
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);
print_r($result);
结果:
Array (
[red] => a
[green] => b
[blue] => c
[yellow] => d
)
4.in_array(search,array,type):搜索数组中是否存在指定的值
例:
$people = array("Bill", "Steve", "Mark", "David");
if (in_array("Mark", $people)){
echo "匹配已找到";
}else{
echo "匹配未找到";
}
结果:
匹配已找到
5.array_search(value,array,strict):在数组中搜索某个键值,并返回对应的键名
例:
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
结果:
a
6.array_key_exists(key,array):检查某个数组中是否存在指定的键名,如果键名存在则返回 true
例:
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (array_key_exists("Volvo",$a)){
echo "键存在!";
}else{
echo "键不存在!";
}
结果:
键存在!
7.isset($arr): 检测变量是否设置
例:
$a = "test";
var_dump(isset($a));
结果:
true
数组的内部指针
1.current(array):返回数组中的当前元素的值
例:
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "
";
结果:
Peter
2.pos(array):返回数组中的当前元素的值(该函数是current()函数的别名
)
例:
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo pos($people) . "
";
结果:
Peter
3.key(array):返回数组内部指针当前指向元素的键名
例:
$people = array("Bill", "Steve", "Mark", "David");
echo "键的当前位置是:" . key($people);
结果:
键的当前位置是:0
4.prev(array):将内部指针指向数组中的上一个元素
例:
$people = array("Bill", "Steve", "Mark", "David");
echo current($people) . "
";
echo next($people) . "
";
echo prev($people);
结果:
Bill
Steve
Bill
5.next(array):将内部指针指向数组中的下一个元素
例:
$people = array("Bill", "Steve", "Mark", "David");
echo current($people) . "
";
echo next($people);
结果:
Bill
Steve
6.end(array):将数组内部指针指向最后一个元素,并返回该元素的值
例:
$people = array("Bill", "Steve", "Mark", "David");
echo current($people) . "
";
echo end($people);
结果:
Bill
David
7.reset(array):将内部指针指向数组中的第一个元素
例:
$people = array("Bill", "Steve", "Mark", "David");
echo current($people) . "
";
echo next($people) . "
";
echo reset($people);
结果:
Bill
Steve
Bill
8.each(array):返回当前元素的键名和键值,并将内部指针向前移动
例:
$people = array("Bill", "Steve", "Mark", "David");
print_r (each($people));
结果:
Array (
[1] => Bill
[value] => Bill
[0] => 0
[key] => 0
)
9.list:用于在一次操作中给一组变量赋值
例:
$my_array = array("Dog","Cat","Horse");
list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
结果:
I have several animals, a Dog, a Cat and a Horse.
数组和变量之间的转换
1.extract(array,extract_rules,prefix):用于把数组中的元素转换成变量导入到当前文件中,键名当作变量名,值作为变量值
例:
$a = "Original";
$my_array = array("a" => "Cat", "b" => "Dog", "c" => "Horse");
extract($my_array, EXTR_PREFIX_SAME, "dup");
echo "\$a = $a; \$b = $b; \$c = $c; \$dup_a = $dup_a";
结果:
$a = Original; $b = Dog; $c = Horse; $dup_a = Cat
2.compact(var1,var2...):创建包含变量名和它们的值的数组
例:
$firstname = "Bill";
$lastname = "Gates";
$age = "60";
$name = array("firstname", "lastname");
$result = compact($name, "location", "age");
print_r($result);
结果:
Array (
[firstname] => Bill
[lastname] => Gates
[age] => 60
)
二、数组的分段和填充
数组的分段
1.array_slice(array,start,length,preserve):在数组中根据条件取出一段值
例:
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
结果:
Array (
[0] => blue
[1] => yellow
[2] => brown
)
2.array_splice(array,start,length,array):从数组中移除选定的元素,并用新元素取代它
例:
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"purple","b"=>"orange");
array_splice($a1,0,2,$a2);
print_r($a1);
结果:
Array (
[0] => purple
[1] => orange
[c] => blue
[d] => yellow
)
分割多个数组
array_chunk(array,size,preserve_key):把数组分割为带有两个元素的数组,并保留原始数组中的键名
参数参考:http://www.w3school.com.cn/php/func_array_chunk.asp
例:
$age=array("Bill"=>"60","Steve"=>"56","Mark"=>"31","David"=>"35");
print_r(array_chunk($age,2,true));
结果:
Array (
[0] => Array (
[Bill] => 60
[Steve] => 56
)
[1] => Array (
[Mark] => 31
[David] => 35
)
)
数组的填充
array_pad(array,size,value):将指定数量的带有指定值的元素插入到数组中
参数参考:http://www.w3school.com.cn/php/func_array_pad.asp
例:
$a=array("red","green");
print_r(array_pad($a,5,"blue"));
结果:
Array (
[0] => red
[1] => green
[2] => blue
[3] => blue
[4] => blue
)
三、数组与栈
1.array_push(array,value1,value2...):向第一个参数的数组尾部添加一个或多个元素(入栈),然后返回新数组的长度
例:
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
结果:
Array (
[0] => red
[1] => green
[2] => blue
[3] => yellow
)
2.array_pop(array): 删除数组中的最后一个元素(出栈)
例:
$a=array("red","green","blue");
array_pop($a);
print_r($a);
结果:
Array (
[0] => red
[1] => green
)
四、数组与列队
1.array_shift(array):数组中的第一个元素移出并作为结果返回(数组长度减1,其他元素向前移动一位,数字键名改为从零技术,文字键名不变)
例:
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);
print_r ($a);
结果:
red
Array (
[b] => green
[c] => blue
)
2.array_unshift(array,value1,value2,value3...):在数组的开头插入一个或多个元素,该函数会返回数组中元素的个数
例:
$a=array("a"=>"red","b"=>"green");
array_unshift($a,"blue");
print_r($a);
结果:
Array (
[0] => blue
[a] => red
[b] => green
)
五、回调函数
1.array_walk(array,myfunction,userdata...):使用用户函数对数组中的每个成员进行处理(第三个参数传递给回调函数function)
例:
function myfunction($value,$key,$p){
echo "$key $p $value
";
}
$a=array("a"=>"red","b"=>"green","c"=>"blue");
array_walk($a,"myfunction","has the value");
结果:
a has the value red
b has the value green
c has the value blue
2.array_map(myfunction,array1,array2,array3...): 将用户自定义函数作用到数组中的每个值上,并返回用户自定义函数作用后的带有新值的数组
例:
function myfunction($v){
return($v*$v);
}
$a=array(1,2,3,4,5);
print_r(array_map("myfunction",$a));
结果:
Array (
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
3.array_filter(array,callbackfunction): 用回调函数过滤数组中的值。如果回调函数返回 true,则把输入数组中的当前键值返回结果数组中。数组键名保持不变
例:
function test_odd($var){
return($var & 1);
}
$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd"));
结果:
Array ( [3] => 3 )
4.array_reduce(array,myfunction,initial): 用回调函数迭代地将数组简化为单一的值
例:
function myfunction($v1,$v2){
return $v1 . "-" . $v2;
}
$a=array("Dog","Cat","Horse");
print_r(array_reduce($a,"myfunction"));
结果:
-Dog-Cat-Horse
六、数组的排序
通过元素值对数组排序
1.sort(array,sortingtype): 对索引数组进行升序排序,第二个参数为按什么方式排序。忽略键名的数组排序
例:
$cars=array("Volvo","BMW","Toyota");
sort($cars);
$clength=count($cars);
for($x=0;$x<$clength;$x++){
echo $cars[$x];
echo "
";
}
结果:
BMW
Toyota
Volvo
2.rsort(array,sortingtype):对数值数组进行降序排序,第二个参数为按什么方式排序。忽略键名的数组排序
例:
$cars=array("Volvo","BMW","Toyota");
rsort($cars);
$clength=count($cars);
for($x=0;$x<$clength;$x++){
echo $cars[$x];
echo "
";
}
结果:
Volvo
Toyota
BMW
3.usort(array,myfunction): 通过用户自定义的比较函数对数组进行排序(function中有两个参数,0表示相等,正数表示第一个大于第二个,负数表示第一个小于第二个)。忽略键名的数组排序
例:
function my_sort($a,$b){
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$a=array(4,2,8,6);
usort($a,"my_sort");
$arrlength=count($a);
for($x=0;$x<$arrlength;$x++){
echo $a[$x];
echo "
";
}
结果:
2
4
6
8
4.asort(array,sortingtype): 对关联数组按照键值进行升序排序,第二个参数为按什么方式排序。保留键名的数组排序
例:
$age=array("Bill"=>"60","Steve"=>"56","Mark"=>"31");
asort($age);
foreach($age as $x=>$x_value){
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
结果:
Key=Mark, Value=31
Key=Steve, Value=56
Key=Bill, Value=60
5.arsort(array,sortingtype): 对关联数组按照键值进行降序排序,第二个参数为按什么方式排序。保留键名的数组排序
例:
$age=array("Bill"=>"60","Steve"=>"56","Mark"=>"31");
arsort($age);
foreach($age as $x=>$x_value){
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
结果:
Key=Bill, Value=60
Key=Steve, Value=56
Key=Mark, Value=31
6.uasort(array,myfunction): 使用用户自定义的比较函数对数组排序,并保持索引关联(不为元素分配新的键)。保留键名的数组排序
例:
function my_sort($a,$b){
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$arr=array("a"=>4,"b"=>2,"c"=>8,d=>"6");
uasort($arr,"my_sort");
foreach($arr as $x=>$x_value){
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
结果:
Key=b, Value=2
Key=a, Value=4
Key=d, Value=6
Key=c, Value=8
通过键名对数组排序
1.ksort(array,sortingtype):对关联数组按照键名进行升序排序
例:
$age=array("Bill"=>"60","Steve"=>"56","mark"=>"31");
ksort($age);
foreach($age as $x=>$x_value){
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
结果:
Key=Bill, Value=60
Key=Steve, Value=56
Key=mark, Value=31
2.krsort(array,sortingtype):对关联数组按照键名进行降序排序
例:
$age=array("Bill"=>"60","Steve"=>"56","mark"=>"31");
krsort($age);
foreach($age as $x=>$x_value){
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
结果:
Key=mark, Value=31
Key=Steve, Value=56
Key=Bill, Value=60
3.uksort(array,myfunction):通过用户自定义的比较函数对数组按键名进行排序
例:
function my_sort($a,$b){
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$arr=array("a"=>4,"b"=>2,"c"=>8,d=>"6");
uksort($arr,"my_sort");
foreach($arr as $x=>$x_value){
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
结果:
Key=a, Value=4
Key=b, Value=2
Key=c, Value=8
Key=d, Value=6
自然排序法排序
1.natsort(array):自然排序(忽略键名)
例:
$temp_files = array("temp15.txt","temp10.txt",
"temp1.txt","temp22.txt","temp2.txt");
sort($temp_files);
echo "标准排序:";
print_r($temp_files);
echo "
";
natsort($temp_files);
echo "自然排序:";
print_r($temp_files);
结果:
标准排序:
Array (
[0] => temp1.txt
[1] => temp10.txt
[2] => temp15.txt
[3] => temp2.txt
[4] => temp22.txt
)
自然排序:
Array (
[0] => temp1.txt
[3] => temp2.txt
[1] => temp10.txt
[2] => temp15.txt
[4] => temp22.txt
)
2.natcasesort(array): 自然排序(忽略大小写,忽略键名)
例:
$temp_files = array("temp15.txt","Temp10.txt",
"temp1.txt","Temp22.txt","temp2.txt");
natsort($temp_files);
echo "自然排序:";
print_r($temp_files);
echo "
";
natcasesort($temp_files);
echo "不区分大小写的自然排序:";
print_r($temp_files);
结果:
自然排序:
Array(
[0] => Temp10.txt
[1] => Temp22.txt
[2] => temp1.txt
[4] => temp2.txt
[3] => temp15.txt
)
不区分大小写的自然顺序:
Array(
[2] => temp1.txt
[4] => temp2.txt
[0] => Temp10.txt
[3] => temp15.txt
[1] => Temp22.txt
)
七、数组的计算
数组元素的求和
array_sum(array): 返回数组中所有值的和
例:
$a=array(5,15,25);
echo array_sum($a);
结果:
45
数组的合并
1.array_merge(array1,array2,array3...):把一个或多个数组合并为一个数组
例:
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
结果:
Array (
[0] => red
[1] => green
[2] => blue
[3] => yellow
)
2.array_merge_recursive(array1,array2,array3...):把一个或多个数组合并为一个数组
array_merge_recursive() 不会进行键名覆盖,而是将多个相同键名的值递归组成一个数组
例:
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge_recursive($a1,$a2));
结果:
Array (
[a] => red
[b] => Array ( [0] => green [1] => yellow )
[c] => blue
)
数组的差集
1.array_diff(array1,array2,array3...):返回两个数组的差集数组
例:
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
print_r($result);
结果:
Array ( [d] => yellow )
2.array_diff_assoc(array1,array2,array3...):用于比较两个(或更多个)数组的键名和键值 ,并返回差集
例:
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff_assoc($a1,$a2);
print_r($result);
结果:
Array (
[a] => red
[b] => green
[c] => blue
[d] => yellow
)
数组的交集
1.array_intersect(array1,array2,array3...):用于比较两个(或更多个)数组的键值,并返回交集
例:
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_intersect($a1,$a2);
print_r($result);
结果:
Array (
[a] => red
[b] => green
[c] => blue
)
2.array_intersect_assoc(array1,array2,array3...):用于比较两个(或更多个)数组的键名和键值,并返回交集
例:
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","g"=>"blue");
$a3=array("a"=>"red","b"=>"green","g"=>"blue");
$result=array_intersect_assoc($a1,$a2,$a3);
print_r($result);
结果:
Array (
[a] => red
[b] => green
)
八、其他的数组函数
1.range(low,high,step):创建一个包含指定范围的元素的数组
例:
$number = range(0,5);
print_r ($number);
结果:
Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
2.array_unique(array):移除数组中的重复的值,并返回结果数组
例:
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
结果:
Array (
[a] => red
[b] => green
)
3.array_reverse(array,preserve): 将原数组中的元素顺序翻转,创建新的数组并返回
如果第二个参数指定为 true,则元素的键名保持不变,否则键名将丢失
例:
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));
结果:
Array (
[c] => Toyota
[b] => BMW
[a] => Volvo
)
4.array_rand(array,number): 从数组中随机选出一个或多个元素
例:
$a=array("red","green","blue","yellow","brown");
$random_keys=array_rand($a,3);
echo $a[$random_keys[0]]."
";
echo $a[$random_keys[1]]."
";
echo $a[$random_keys[2]];
结果:
red
yellow
brown
5.shuffle(array): 将数组的顺序打乱
例:
$my_array = array("red","green","blue","yellow","purple");
shuffle($my_array);
print_r($my_array);
结果:
Array (
[0] => blue
[1] => green
[2] => purple
[3] => red
[4] => yellow
)