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


Posted in PHP onDecember 07, 2016

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

array_combine

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

案例:

<?php
$a = array("one","two","three");
$b = array("一","二","三");
$c = array_combine($a,$b);
print_r($c);
/**结果
*Array ( [one] => 一 [two] => 二 [three] => 三 )
*/

array_chunk

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

<?php
$input_array = array("a"=>"apple","b"=>"blue","c","d","e");
echo "<pre>";
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2,True));
echo "</pre>";
/**结果
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
$input_array = array("a"=>"apple","b"=>"blue","c","d","e");
echo "<pre>";
print_r(array_count_values($input_array));
echo "</pre>";
/**结果
Array
(
 [apple] => 1
 [blue] => 1
 [c] => 1
 [d] => 1
 [e] => 1
)
*/

array_diff

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

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

array_map

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

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

array_merge

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

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

<?php
$array1 = array("color"=>"red",2,4);
$array2 = array("a","b","color"=>"green","shape"=>"trapezoid",4);
$result1 = array_merge($array1,$array2);
$result2 = array_merge_recursive($array1,$array2);
echo "<pre>";
print_r($result1);
print_r($result2);
echo "</pre>";
/**结果
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
$stack = array("orange","banana","apple","1");
$last = array_pop($stack);
echo "<pre>";
print_r($stack);
print_r($last);
echo "</pre>";
/**结果
Array
(
 [0] => orange
 [1] => banana
 [2] => apple
)
1
*/

array_push

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

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

array_rand

功能:获取随机的键名

<?php
$input = array("orange","banana","apple","red","blue");
$rand = array_rand($input,2);;
print_r($rand);
$rand = array_rand($input,3);
print_r($rand);
/**结果
Array
(
 [0] => 1
 [1] => 4
)
Array
(
 [0] => 0
 [1] => 1
 [2] => 3
)
*/

array_search

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

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

array_shift

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

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

array_unique

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

<?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

功能:进行正向排序

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

krsort

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

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

ksort

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

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

rsort

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

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

sort

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

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

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

PHP 相关文章推荐
一个简单的PHP投票程序源码
Mar 11 PHP
PHP 面向对象实现代码
Nov 11 PHP
探讨:如何使用PHP实现计算两个日期间隔的年、月、周、日数
Jun 13 PHP
CI(CodeIgniter)框架中的增删改查操作
Jun 10 PHP
PHP实现文件下载断点续传详解
Oct 15 PHP
PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别
Nov 20 PHP
PHP网站开发中常用的8个小技巧
Feb 13 PHP
Linux平台php命令行程序处理管道数据的方法
Nov 10 PHP
php_pdo 预处理语句详解
Nov 21 PHP
简单实现php上传文件功能
Sep 21 PHP
Laravel (Lumen) 解决JWT-Auth刷新token的问题
Oct 24 PHP
PHP中的输出echo、print、printf、sprintf、print_r和var_dump的示例代码
Dec 01 PHP
php常用字符串String函数实例总结【转换,替换,计算,截取,加密】
Dec 07 #PHP
php自定义时间转换函数示例
Dec 07 #PHP
php微信公众平台开发(四)回复功能开发
Dec 06 #PHP
php微信公众平台开发(三)订阅事件处理
Dec 06 #PHP
php微信公众平台示例代码分析(二)
Dec 06 #PHP
php微信公众平台开发(一) 配置接口
Dec 06 #PHP
php简单计算年龄的方法(周岁与虚岁)
Dec 06 #PHP
You might like
第六节--访问属性和方法
2006/11/16 PHP
php download.php实现代码 跳转到下载文件(response.redirect)
2009/08/26 PHP
PHP对字符串的递增运算分析
2010/08/08 PHP
PHP中全局变量global和$GLOBALS[]的区别分析
2012/08/06 PHP
基于php设计模式中工厂模式详细介绍
2013/05/15 PHP
一个简单且很好用的php分页类
2013/10/26 PHP
php将图片保存为不同尺寸图片的图片类实例
2015/03/30 PHP
RR vs IO BO3 第一场2.13
2021/03/10 DOTA
Nodejs异步回调的优雅处理方法
2014/09/25 NodeJs
创建自己的jquery表格插件
2015/11/25 Javascript
Angularjs中使用layDate日期控件示例
2017/01/11 Javascript
Bootstrap与Angularjs的模态框实例代码
2017/08/03 Javascript
详解React 在服务端渲染的实现
2017/11/16 Javascript
详解angular部署到iis出现404解决方案
2018/08/14 Javascript
原生JS实现简单的无缝自动轮播效果
2018/09/26 Javascript
微信小程序常见页面跳转操作简单示例
2019/05/01 Javascript
小程序使用watch监听数据变化的方法详解
2019/09/20 Javascript
vue resource发送请求的几种方式
2019/09/30 Javascript
JS实现拖动模糊框特效
2020/08/25 Javascript
[44:40]KG vs LGD 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
python类继承用法实例分析
2015/05/27 Python
Python脚本实现12306火车票查询系统
2016/09/30 Python
python 图片二值化处理(处理后为纯黑白的图片)
2019/11/01 Python
Python通过TensorFLow进行线性模型训练原理与实现方法详解
2020/01/15 Python
Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释
2020/01/25 Python
Selenium webdriver添加cookie实现过程详解
2020/08/12 Python
python获取天气接口给指定微信好友发天气预报
2020/12/28 Python
amazeui页面分析之登录页面的示例代码
2020/08/25 HTML / CSS
瑞典快乐袜子:Happy Socks
2018/02/16 全球购物
意大利独特而优质的家居用品:Fazzini
2018/12/05 全球购物
品学兼优的大学生自我评价
2013/09/20 职场文书
《要下雨了》教学反思
2014/02/17 职场文书
个性发展自我评价2015
2015/03/09 职场文书
销售开票员岗位职责
2015/04/15 职场文书
2015年售票员工作总结
2015/04/29 职场文书
Python中异常处理用法
2021/11/27 Python