1.array_slice()
从数组中取出一段
$data=array("Linux","Apache","MySQL","PHP");
$data = array_slice($data,2); //取出数组中的前两个
print_r($data); //Array ( [0] => MySQL [1] => PHP )
$data = array_slice($data,1,2); ////取下标为1、2的元素
print_r($data); //Array ( [0] => Apache [1] => MySQL )
$data = array_slice($data,1,2,true); //取下标为1、2的元素 保留之前的键
print_r($data); //Array ( [1] => Apache [2] => MySQL )
2.array_splice()
把数组中的一部分去掉并用其它值取代
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green","blue", "black", "maroon")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green","blue", "purple", "yellow");
3.array_combine()
创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
$a1=array("OS","WebServer","DataBase","Language");
$a2=array("Linux","Apache","MySQL","PHP");
print_r(array_combine($a1,$a2));//第一个参数作为键名,第二个作为值来合并
//Array ( [OS] => Linux [WebServer] => Apache [DataBase] => MySQL [Language] => PHP )
4.array_merge()
合并一个或多个数组
$a3=array("OS","WebServer","DataBase","Language");
$a4=array("Linux","Apache","MySQL","PHP");
$a5=$a3+$a4;
print_r($a5);//因为两个数组下标重复所以显示这样
//Array ( [0] => OS [1] => WebServer [2] => DataBase [3] => Language )
print_r(array_merge($a3,$a4));//合并并重新索引
//Array ( [0] => OS [1] => WebServer [2] => DataBase [3] => Language [4] => Linux [5] => Apache [6] => MySQL [7] => PHP )
5.array_intersect()
计算数组的交集
$a7=array("OS","WebServer","DataBase","Language",1,2,3);
$a8=array("Linux","Apache","MySQL","PHP",2,3,4);
print_r(array_intersect($a7,$a8));//Array ( [5] => 2 [6] => 3 )
6.array_diff()
计算数组的差集
$a9=array(1,2,3,4);
$a10=array(3,4,5,6);
print_r(array_diff($a9,$a10));//Array ( [0] => 1 [1] => 2 )
//返回第一个数组跟第二个相差的元素
已有 0 条评论