php常用数组函数使用示例大全整理

 1,两个数组合并场景

$a1 = ['a'=>'a1','b'=>'b1'];
$a2 = ['b'=>'b2','c'=>'c2'];
$a12 = array_merge($a1, $a2);//结果是:['a'=>'a1','b'=>'b2','c'=>'c2'];
$a21 = array_merge($a2, $a1);//结果是:['b'=>'b1','c'=>'c2','a'=>'a1'];

2,二维数组根据一个的指定字段排序

$disa=array_column($list,'distance');
array_multisort($disa, SORT_ASC, $list);//按照字段distance升序排序

3,统计数组值的个数,只能为string 和 integer 类型

$list = array(2,4,99,'php',4,2,'php',4);
$listnum=array_count_values($list);
print_r($listnum);die;
//Array ( [2] => 2 [4] => 3 [99] => 1 [php] => 2 )

4,读表存表时,字符串转数组,数组转字符串

$info['remarks'] = json_decode(stripslashes(html_entity_decode($info['remarks'])), true);//转数组
$info['remarks'] = json_encode($remarks_arr, JSON_UNESCAPED_UNICODE);//转字符串

5,数组排序,并读取第一个元素及最后一个元素

$datelist = array('2022-01-31','2022-01-10','2022-02-01','2021-12-28');
$adate0 = reset($datelist);//数组第一个元素,数组不变
$adate1 = end($datelist);//数组最后一个元素,数组不变

$first = array_shift($datelist);//数组第一个元素,数组变化
$last = array_pop($datelist);//数组最后一个元素,数组变化

6,数组去重、数组过滤、数组下标重新排列

//数组去重复
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));

//数组过滤空数值并数组下标重新排列
$aaa = array('aa','','bb');
$bbb = array_filter($aaa);//数组过滤
print_r($bbb);die;//结果Array ( [0] => aa [2] => bb )

//二维数组提取contract_no字段,然后转换为字符串,然后再转换为数组,然后去重复加过滤
$contract_no = array_column($data['data'],'contract_no');//
$contract_no = implode(',',$contract_no);
$contract_no = explode(',',$contract_no);
$contract_no = array_filter(array_unique($contract_no));//去重复加过滤

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜爱cpp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值