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));//去重复加过滤