array("..."," ..."...);
用途:生成一个数组;
>>例子:$a=array("Dog","Cat","Horse");
print_r($a);
输出:"0"=>"Dog","1"=>"Cat","2"=>"Horse";
array_combine(array1,array2);
用途:生成一个数组,用一个数组的值作为键名,另一个数组的值作为键值;若array1和array2 的键值对个数不一,将会发出警告!
>>例子:$a1=array("a","b","c","d");
$a2=array("Cat","Dog","Horse","Cow");
print_r(array_combine($a1,$a2));
=>输出:Array ("a"=>"Cat","b"=>"Dog" ...)
range(int1,int2[,int length]);
用途:创建并返回一个包含指定范围的元素的索引数组;
>>例子:$number = range(0,50,10);
print_r ($number);
输出:Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 )
compact(var1,var2...);
用途:创建一个由参数所带变量组成的数组;
>>例子: $firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$result = compact("firstname", "lastname", "age");
print_r($result);
输出:Array ( [firstname] => Peter [lastname] => Griffin [age] => 38 ) //若$result至于开头,则输出Array();
array_fill(start,number,value);
用途:用给定的value填充(值生成)数组
>>例子:$a=array_fill(2,3,"Dog");
print_r($a);
输出:Array ( [2] => Dog [3] => Dog [4] => Dog )
array_chunk(array,size,preserve_key);
//说明:preserve值默认false,每个结果数组从零开始生成新的索引数组;为true时,将保留原始数组中的键名!
用途:把一个数组分割为新的数组块
>>例子:$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 ) )
array_merge(array1,array2...);
用途:把两个及以上的数组合并成一个数组
//说明:如果数组是数字索引的,则键名会以连续方式重新索引。
>>例子:$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 )
array_slice(array,offset,length,preserve);
用途:在数组中根据条件取出一段值并返回;
//说明:preserve值默认false,每个结果数组从零开始生成新的索引数组;为true时,将保留原始数组中的键名!
>>例子:$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1,2));
输出:Array ( [0] => Cat [1] => Horse )
array_diff(array,array ...);
//说明:返回的数组中键名不变
用途:返回两个数组的差集数组,即用一个或任意多个数组与第一个数组进行值比较
>>例子:$a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2=array(3=>"Horse",4=>"Dog",5=>"Fish");
print_r(array_diff($a1,$a2));
输出:Array ( [0] => Cat )
array_intersect(array,array ...);
用途:返回两个以上数组的交集【键值】数组,结果数组包含了所有在被比较数组中,也同时出现在所有其他参数数组中的值,键名保留不变。
>>例子:$a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2=array(3=>"Horse",4=>"Dog",5=>"Fish");
print_r(array_intersect($a1,$a2));
输出:Array ( [1] => Dog [2] => Horse )
array_search("array_value",array[,strict]);
//说明:strict表示可能的值,默认为false,为true时还将在数组中检查给定值的类型!
用途:在数组中查找一个键值,成功返回键名,失败返回false
>>例子:$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
echo array_search("Dog",$a);
输出:a
in_array(value,array,type);
//type默认为false,为true时将检查搜索的数据与数组的值的类型是否相同!
用途:在数组中区分大小写的搜索给定的值,返回true或false
>>例子:$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn",$people))
{
echo "Match found";
}else{
echo "Match not found";
}
输出:Match found
array_splice(array,offset,length,array);
//第一个array为规定的数组,第二个为被移除元素由此数组元素替代;如果 offset 为正,则从输入数组中该值指定的偏移量开始移除。如果 offset 为负,则从输入数组末尾倒数该值指定的偏移量开始移除。如果指定了 length 且为负值,则移除从 offset 到数组末尾倒数 length 为止中间所有的元素。
用途:把数组中一部分删除用其他值替代
>>例子:$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
$a2=array(0=>"Tiger",1=>"Lion");
array_splice($a1,0,2,$a2);
print_r($a1);
输出:Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird )
array_sum(array);
用途:返回数组中所有值的总和,字符串将转换为int型再运算
>>例子:$a=array(0=>"5",1=>"15",2=>"25","a"=>"3b");
echo array_sum($a);
输出:48
array_key_exists(key,array);
用途:判断某个数组中是否存在指定的key,返回true或false
>>例子:$a=array("a"=>"Dog","b"=>"Cat");
if (array_key_exists("a",$a))
{
echo "Key exists!";
}else{
echo "Key does not exist!";
}
输出:Key exists!
key(array);
用途:返回数组内部指针当前指向元素的键名
>>例子:$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo "The key from the current position is: " . key($people);
输出:The key from the current position is: 0
current(array); <=> pos(array);
用途:返回数组当前的元素[键值]
>>例子:$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br />";
输出:Peter
next(array);
用途:把当前数组内部元素的指针移动到下一个元素的位置并返回当前元素的值
//说明:如果移动指针的结果超出了数组单元的末端,则返回 FALSE。如果数组包含空的单元,或者单元的值是 0 则该函数碰到这些单元也返回 FALSE
>>例子;$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br />";
echo next($people);
输出:Peter
Joe
each(array);
用途:生成一个由数组当前内部指针所指向的元素的键名和键值组成的数组,并把内部指针向前移动。返回的数组中包括的四个元素:键名为 0,1,key 和 value。单元 0 和 key 包含有数组单元的键名,1 和 value 包含有数据。
//说明:如果内部指针越过了数组范围,本函数将返回 FALSE。
>>例子:$people = array("Peter", "Joe", "Glenn", "Cleveland");
print_r (each($people));
输出:Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 )
prev(array);
用途:把当前数组内部元素的指针移动到上一个元素的位置并返回当前元素的值
//说明:如果数组包含空的单元,或者单元的值是 0 则该函数碰到这些单元也返回 FALSE。要正确遍历可能含有空单元或者单元值为 0 的数组
>>例子:$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br />";
echo next($people) . "<br />";
echo prev($people);
输出:Peter
Joe
Peter
end(array);
用途:把数组内部指针指向最后一个元素,若成功则返回该元素的值
>>例子:$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo end($people);
输出:Cleveland
reset(array);
用途:重置数组内部指针,使其指向第一个元素并返回该元素的值
>>例子:$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br />";
echo next($people) . "<br />";
echo reset($people);
输出:Peter
Joe
Peter
list(var1,var2 ... );
用途:将数组中的元素给一组变量赋值,本质是一种语言结构,不属于函数
//说明:该函数仅用于数字索引的数组,且假定数字索引从0开始
>>例子:$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.
array_shift(array);
用途:删除数组中的第一个元素并返回被删除元素的值
//说明:如果键是数字的,所有元素都将获得新的键,从 0 开始,并以 1 递增;若为关联数组,键名不变
>>例子:$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
$b=array(0=>"Dog",1=>"Cat",2=>"Horse");
echo array_shift($a);
print_r ($a);
echo "<br />";
echo array_shift($b);
print_r ($b);
输出:Dog
Array ( [b] => Cat [c] => Horse
Dog
Array ( [0] => Cat [1] => Horse )
array_pop(array);
用途:删除数组中的最后一个元素的值,但返回被删除元素的值
//说明:如果键是数字的,所有元素都将获得新的键,从 0 开始,并以 1 递增;若为关联数组,键名不变
>>例子:$a=array("Dog","Cat","Horse");
$result=array_pop($a);
echo $result;
print_r($a);
输出:Horse
Array ( [0] => Dog [1] => Cat )
array_unshift(array,value1,value2 ... );
用途:在数组开头插入一个或多个元素,返回数组中元素的个数!
>>例子:$a=array("a"=>"Cat","b"=>"Dog");
$num=array_unshift($a,"Horse");
echo $num;
print_r($a);
输出:3
Array ( [0] => Horse [a] => Cat [b] => Dog )
array_push(array,value1,value2 ...);
用途:向数组的末尾压入一个或多个元素[入栈],返回数组中元素的个数!
//说明:即使数组中有字符串键名,您添加的元素也始终是数字键。如果用 array_push() 来给数组增加一个单元,还不如用 $array[] =,因为这样没有调用函数的额外负担。还有,若第一个参数非数组则发出警告!
>>例子:$a=array("a"=>"Dog","b"=>"Cat");//$a=array("Dog","Cat");
array_push($a,"Horse","Bird");
print_r($a);
输出:Array ( [a] => Dog [b] => Cat [0] => Horse [1] => Bird ) //Array ( [0] => Dog [1] => Cat [2] => Horse [3] => Bird )
shuffle(array);
用途:将数组打乱,保留键值,键名重新索引,成功返回true,反之返回false
>>例子:$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
print_r($my_array);
输出:Array ( [0] => Cat [1] => Horse [2] => Dog )
count(array[,mode]);
//说明:mode默认0,不检查多维数组,为1时检测多维数组
用途:统计数组中键值对的总数
>>例子:$people = array("Peter", "Joe", "Glenn", "Cleveland");
ehco count($people);
输出:4
array_flip(array);
用途:返回一个反转键值对之后的数组,若同一个值择最后的键名为准;若键值非字符串或整数,函数报错!
>>例子:$a=array(0=>"Dog",1=>"Cat",2=>"Horse","a"=>"Horse");
print_r(array_flip($a));
输出:Array ( [Dog] => 0 [Cat] => 1 [Horse] => a )
array_reverse(array[,preserve]);
//preserve默认true保留原始数组的键名,为false时不保留
用途:将数组顺序翻转[区别于array_flip],创建新的数组
>>例子:$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r(array_reverse($a));
输出:Array ( [c] => Horse [b] => Cat [a] => Dog )
array_keys(array[,value[,strict]]);
//说明:value为指定的键值值;strict默认false,不依赖类型,为true时根据类型[类似==与===]返回带有指定值的键名。
用途:将数组中所有的键名重新组成一个新的索引数组
>>例子:$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a));
print_r(array_keys($a,"Dog"));
输出:Array ( [0] => a [1] => b [2] => c )
Array ( [0] => c)
array_values(array);
用途:将数组中所有的键值重新组成一个新的索引数组
>>例子:$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
print_r(array_values($a));
输出:Array ( [0] => Cat [1] => Dog [2] => Horse )
array_count_values(array);
用途:统计数组中所有键值出现的次数,返回数组,以原数组键值为键名,键值是该键名在原数组出现的次数。
>>例子:$a=array("Cat","Dog","Horse","Dog");
print_r(array_count_values($a));
输出:Array ( [Cat] => 1 [Dog] => 2 [Horse] => 1 )
array_rand(array[,number]);
用途:随机抽取数组中的一个或多个键名!多个键名以数组形式返回!
>>例子:$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
$b=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r(array_rand($a,1));
print_r(array_rand($b,2));
输出:b
Array ( [0] => c [1] => b )
array_unique(array);
//说明:先将值作为字符串排序,然后对每个值只保留第一个遇到的键名,接着忽略所有后面的键名。这并不意味着在未排序的 array 中同一个值的第一个出现的键名会被保留。
用途:删除重复键值的键值对,返回剩余的键名不变的数组
>>例子:$a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
print_r(array_unique($a));
输出:Array ( [a] => Cat [b] => Dog )