<?php $input = array("a"=>"green","red","b"=>"green","blue","c"=>"red"); $result = array_unique($input); print_r($result); echo "<br>"; print_r($input); /**结果 Array ( [a] => green [0] => red [1] => blue ) Array ( [a] => green [0] => red [b] => green [1] => blue [c] => red ) */
array_slice
功能:从数组中取出部分元素
<?php $input = array("a","b","c","d","e"); $output = array_slice($input,2);//第二个参数没有时,表示取到最后一个元素 print_r($output); echo "<br>"; $output = array_slice($input,-2,1);//第二个参数是正数时,表示个数;倒数第一个是-1,倒数第二个是-2 print_r($output); echo "<br>"; $output = array_slice($input,0,3); print_r($output); echo "<br>"; $output = array_slice($input,2,-1);//第二个参数是负数时,表示位置,取到哪一位,不包括本身 print_r($output); echo "<br>"; $output = array_slice($input,2,-1,true);//第三个参数为true时,保留原有的键值 print_r($output); echo "<br>"; /**结果 Array ( [0] => c [1] => d [2] => e ) Array ( [0] => d ) Array ( [0] => a [1] => b [2] => c ) Array ( [0] => c [1] => d ) Array ( [2] => c [3] => d ) */
count
功能:返回数组元素个数,元素为数组的算一个
<?php $input = array("a","b","c",array("d","e")); $count = count($input); echo $count; echo "<br>"; $input = array("a","b","c","d","e"); $count = count($input); echo $count; /**结果 4 5 */
current
功能:获取当前的指针指向元素
<?php $array = array("foot","bike","car","plane"); $result = current($array); echo $result."<br>"; next($array);//使指针指向下一个元素 $result = current($array); echo $result."<br>"; prev($array);//使指针指向前一个元素 $result = current($array); echo $result."<br>"; end($array);//使指针指向最后一个元素 $result = current($array); echo $result."<br>"; /**结果 foot bike foot plane */
in_array
功能:检验某值是否存在数组中,有返回True,没有返回False
<?php $os_list = array("Mac","NT","Irix","Linux"); if(in_array("Irix",$os_list)){ echo "当前操作系统列表中存在Irix"; }else{ echo "当前操作系统列表中不存在Irix"; } echo "<br>"; if(in_array("mac",$os_list)){ echo "当前操作系统列表中存在mac"; }else{ echo "当前操作系统列表中不存在mac"; } echo "<br>"; /**结果 当前操作系统列表中存在Irix 当前操作系统列表中不存在mac */
list
功能:将数组中的信息赋值给多个变量
<?php $info = array("red","blue","green"); list($flag,$sky,$grassland) = $info; echo "$flag,$sky,$grassland"; echo "<br>"; list($flag,,$grassland) = $info; echo "$flag,$grassland"; echo "<br>"; list(,,$grassland) = $info; echo "$grassland"; echo "<br>"; /**结果 red,blue,green red,green green */
shuffle
功能:打乱数组
<?php $numbers = range(1,5);//生成一个随机数组 print_r($numbers); echo "<br/>"; shuffle($numbers);//打乱数组 print_r($numbers); /**结果 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( [0] => 4 [1] => 1 [2] => 5 [3] => 2 [4] => 3 ) */
array_keys
功能:获取数组的键名,第二个参数可以指定获取某个元素
<?php $array = array(0=>100,"color"=>"red"); print_r(array_keys($array)); echo "<br>"; $array = array("blue","red","green","blue","blue"); print_r(array_keys($array,"blue")); echo "<br>"; $array = array("color"=>array("blue","red","green"),"size"=>array("small","medium","large")); print_r(array_keys($array)); echo "<br>"; /**结果 Array ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => color [1] => size ) */
array_reverse
功能:获取数组的反向
<?php $input = array("php",3.0,array("green","red")); $result = array_reverse($input); //打乱键名 $result_keyed = array_reverse($input,TRUE);//保留键名 print_r($result); print_r($result_keyed); /**结果 Array ( [0] => Array ( [0] => green [1] => red ) [1] => 3 [2] => php ) Array ( [2] => Array ( [0] => green [1] => red ) [1] => 3 [0] => php ) */
arsort
功能:逆向排序,索引不变
<?php $fruits = array( "a"=>"lemon", "b"=>"orange", "c"=>"banana", "d"=>"apple", ); arsort($fruits);//按照字符逆向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } /**结果 b = orange a = lemon c = banana d = apple */
asort
功能:进行正向排序