php常用数组array函数实例总结【赋值,拆分,合并,计算,添加,删除,查询,判断,排序】

本文实例总结了PHP常用数组array函数分享给大家供大家参考,具体如下:

array_combine

功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值

案例:

PHP;"> 一 [two] => 二 [three] => 三 ) */

array_chunk

功能:拆分数组成多个数组

PHP;"> "apple","b"=>"blue","c","d","e"); echo "
";
print_r(array_chunk($input_array,2));
print_r(array_chunk($input_array,2,True));
echo "
"; /**结果 Array ( [0] => Array ( [0] => apple [1] => blue ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) ) Array ( [0] => Array ( [a] => apple [b] => blue ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [2] => e ) ) */

array_count_values

功能统计数组中值出现的次数

PHP;"> "apple","e"); echo "
";
print_r(array_count_values($input_array));
echo "
"; /**结果 Array ( [apple] => 1 [blue] => 1 [c] => 1 [d] => 1 [e] => 1 ) */

array_diff

功能:第一个数组中去掉第二个数组中有的数据,返回剩下的内容作为结果

PHP;"> "apple","e"); $array2 = array("apple","f"); $result = array_diff($array1,$array2); $result2 = array_diff($array2,$array1); echo "
";
print_r($result);//数组1中去掉数组2中剩下的
print_r($result2);//数组2中去掉数组1中剩下的
echo "
"; /**结果 Array ( [b] => blue [2] => e ) Array ( [3] => f ) */

array_map

功能:将回调函数执行到数组中

PHP;"> PHP //定义回调函数 function cube($n){ return ($n*$n*$n); } $a = array(1,3,4,5); $b = array_map("cube",$a); echo "
";
print_r($b);
echo "
"; /**结果 Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 ) */

array_merge

功能:合并一个或多个数组

说明:如果后面有键名相同的会覆盖掉前面的内容,键名为数字的会添加到后面

PHP;"> "red",4); $array2 = array("a","b","color"=>"green","shape"=>"trapezoid",4); $result1 = array_merge($array1,$array2); $result2 = array_merge_recursive($array1,$array2); echo "
";
print_r($result1);
print_r($result2);
echo "
"; /**结果 Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) Array ( [color] => Array ( [0] => red [1] => green ) [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) */

array_pop

功能:剔除数组最后一个元素,返回被剔除的元素内容

PHP;"> PHP $stack = array("orange","banana","apple","1"); $last = array_pop($stack); echo "
";
print_r($stack);
print_r($last);
echo "
"; /**结果 Array ( [0] => orange [1] => banana [2] => apple ) 1 */

array_push

功能:将一个多个单元压入数组末尾,返回之后的数组个数

PHP;"> PHP $stack = array("orange","banana"); $count = array_push($stack,"red","blue"); echo "
";
print_r($stack);
print_r($count);
echo "
"; /**结果 Array ( [0] => orange [1] => banana [2] => apple [3] => red [4] => blue ) 5 */

array_rand

功能获取随机的键名

PHP;"> 1 [1] => 4 ) Array ( [0] => 0 [1] => 1 [2] => 3 ) */

array_search

功能查询数组中的内容,返回键值,如果有多个匹配,返回第一个匹配的内容

PHP;"> "b","red"=>"r","green","r"); $key = array_search('b',$array); echo $key; echo "
"; $key = array_search('r',$array); echo $key; echo "
"; /**结果 blue red */

array_shift

功能:移除开头的元素,与array_pop相反

PHP;"> PHP $fruit = array("milk","orange","apple"); $top = array_shift($fruit); print_r($top); echo "
"; print_r($fruit); /**结果 milk Array ( [0] => orange [1] => banana [2] => apple ) */

array_unique

功能去除数组重复的元素,保留第一个出现的,包括键名和值

PHP;"> "green","b"=>"green","blue","c"=>"red"); $result = array_unique($input); print_r($result); echo "
"; print_r($input); /**结果 Array ( [a] => green [0] => red [1] => blue ) Array ( [a] => green [0] => red [b] => green [1] => blue [c] => red ) */

array_slice

功能:从数组中取出部分元素

PHP;"> PHP $input = array("a","e"); $output = array_slice($input,2);//第二个参数没有时,表示取到最后一个元素 print_r($output); echo "
"; $output = array_slice($input,-2,1);//第二个参数是正数时,表示个数;倒数第一个是-1,倒数第二个是-2 print_r($output); echo "
"; $output = array_slice($input,3); print_r($output); echo "
"; $output = array_slice($input,-1);//第二个参数是负数时,表示位置,取到哪一位,不包括本身 print_r($output); echo "
"; $output = array_slice($input,-1,true);//第三个参数为true时,保留原有的键值 print_r($output); echo "
"; /**结果 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;"> PHP $input = array("a",array("d","e")); $count = count($input); echo $count; echo "
"; $input = array("a","e"); $count = count($input); echo $count; /**结果 4 5 */

current

功能:获取当前的指针指向元素

PHP;"> PHP $array = array("foot","bike","car","plane"); $result = current($array); echo $result."
"; next($array);//使指针指向下一个元素 $result = current($array); echo $result."
"; prev($array);//使指针指向前一个元素 $result = current($array); echo $result."
"; end($array);//使指针指向最后一个元素 $result = current($array); echo $result."
"; /**结果 foot bike foot plane */

in_array

功能:检验某值是否存在数组中,有返回True,没有返回False

PHP;"> PHP $os_list = array("Mac","NT","Irix","Linux"); if(in_array("Irix",$os_list)){ echo "当前操作系统列表中存在Irix"; }else{ echo "当前操作系统列表中不存在Irix"; } echo "
"; if(in_array("mac",$os_list)){ echo "当前操作系统列表中存在mac"; }else{ echo "当前操作系统列表中不存在mac"; } echo "
"; /**结果 当前操作系统列表中存在Irix 当前操作系统列表中不存在mac */

list

功能:将数组中的信息赋值给多个变量

PHP;"> PHP $info = array("red","green"); list($flag,$sky,$grassland) = $info; echo "$flag,$grassland"; echo "
"; list($flag,$grassland"; echo "
"; list(,$grassland) = $info; echo "$grassland"; echo "
"; /**结果 red,blue,green red,green green */

shuffle

功能:打乱数组

PHP;"> PHP $numbers = range(1,5);//生成一个随机数组 print_r($numbers); echo "
"; 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;"> 100,"color"=>"red"); print_r(array_keys($array)); echo "
"; $array = array("blue","blue"); print_r(array_keys($array,"blue")); echo "
"; $array = array("color"=>array("blue","green"),"size"=>array("small","medium","large")); print_r(array_keys($array)); echo "
"; /**结果 Array ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => color [1] => size ) */

array_reverse

功能获取数组的反向

PHP;"> Array ( [0] => green [1] => red ) [1] => 3 [2] => PHP ) Array ( [2] => Array ( [0] => green [1] => red ) [1] => 3 [0] => PHP ) */

arsort

功能:逆向排序,索引不变

PHP;"> "lemon","b"=>"orange","c"=>"banana","d"=>"apple",); arsort($fruits);//按照字符逆向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val
"; } /**结果 b = orange a = lemon c = banana d = apple */

asort

功能:进行正向排序

PHP;"> "lemon",); arsort($fruits);//按照字符逆向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val
"; } echo "

"; asort($fruits);//按照字符正向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val
"; } /**结果 b = orange a = lemon c = banana d = apple d = apple c = banana a = lemon b = orange */

krsort

功能:按照键名进行逆向排序

PHP;"> "lemon",); krsort($fruits);//按照键名逆向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val
"; } /**结果 d = apple c = banana b = orange a = lemon */

ksort

功能:按照键名进行正向排序

PHP;"> "lemon",); ksort($fruits);//按照键名正向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val
"; } /**结果 a = lemon b = orange c = banana d = apple */

rsort

功能:按照值进行逆向排序,键名改变

PHP;"> "lemon",); rsort($fruits);//按照值进行逆向排序或数字,键名改变 foreach($fruits as $key=>$val){ echo "$key = $val
"; } /**结果 0 = orange 1 = lemon 2 = banana 3 = apple */

sort

功能:按照值进行正向排序,键名改变

PHP;"> "lemon",); sort($fruits);//按照值进行逆向排序或数字,键名改变 foreach($fruits as $key=>$val){ echo "$key = $val
"; } /**结果 0 = apple 1 = banana 2 = lemon 3 = orange */

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《

希望本文所述对大家PHP程序设计有所帮助。

相关文章

Hessian开源的远程通讯,采用二进制 RPC的协议,基于 HTTP 传输。可以实现PHP调用Java,Python,C#等多语...
初识Mongodb的一些总结,在Mac Os X下真实搭建mongodb环境,以及分享个Mongodb管理工具,学习期间一些总结...
边看边操作,这样才能记得牢,实践是检验真理的唯一标准.光看不练假把式,光练不看傻把式,边看边练真把式....
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较。 echo与print的区别: (...
在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring exte...
变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变...