合并数组([重复键]覆盖型)
$fruits = array("apple","banana","pear");
$numbered = array("1","2","3");
$cards = array_merge($fruits,$numbered);
print_r($cards);
?>
追加数组([重复键]组合型)
$fruit1 = array("apple"=>"red","banana"=>"yellow");
$fruit2 = array("pear"=>"yellow","apple"=>"green");
$result = array_merge_recursive($fruit1,$fruit2);
print_r($result);
?>
连接数组(键与值连接)
$name = array("apple","banana","orange");
$color = array("red","yellow","orange");
$fruit = array_combine($name,$color);
print_r($fruit);
?>
获取关联数组的Key
key(["apple"=>"hello"]);(单个)
array_keys(["apple"=>"hello","banana"=>"world"]);(多个)
?>
获取关联数组的Value
current(["apple"=>"hello"]);(单个)
array_values(["apple"=>"hello","banana"=>"world"])
?>
数组搜索判断
$fruit="apple";
$fruits=array("apple","banana","orange","pear");
if(in_array($fruit,$fruits) )
echo"$fruit已经在数组中";
?>
关联数组Key搜索判断
$fruit["apple"] ="red";
$fruit["banana"] ="yellow";
$fruit["pear"] ="green";
if(array_key_exists("apple",$fruit)){
printf("apple's color is %s",$fruit["apple"]);
}
?>
数组遍历
foreach(array_expression as$value)
statement
foreach(array_expression as$key=>$value)
statement
?>