当前位置:网站首页>php一维数组合并
php一维数组合并
2022-08-03 00:18:00 【大得369】
$arr1=['1','2','3'];
$arr2=['4',24,'6'];
$all=array_combine($arr1, $arr2);//数组合并函数
var_dump($all);
php中数组的几种合并方法:
array_merge(array1,array2):两个数组中的一维数据合并,二维数组如果key相同,array2中的值会替代array1中的值;
加号:array1+array2:把array2中key对应在array1不存在的key的值添加到array1中(像当于array1你有4个位置,则那4个位置的值不变,array2有5个位置,只把第5个位置的值加到array1中)
array_merge_recursive:与array_merge唯一不同的是,二维数据的值不会替换,是递归合并到一起;<?php
$array1 = array(2,4,"color" => "red");
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
echo "----------------array_merge---------------".PHP_EOL;
print_r($result);
echo "----------------+++++++++++---------------".PHP_EOL;
print_r($array1+$array2);
echo "----------------array_merge_recursive---------------".PHP_EOL;
print_r(array_merge_recursive($array1,$array2));
----------------array_merge---------------
Array
(
[0] => 2
[1] => 4
[color] => green
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
----------------+++++++++++---------------
Array
(
[0] => 2
[1] => 4
[color] => red
[shape] => trapezoid
[2] => 4
)
----------------array_merge_recursive---------------
Array
(
[0] => 2
[1] => 4
[color] => Array
(
[0] => red
[1] => green
)
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)<?php
$array1 = array('a','b');
$array2 = array(1,2);
$result = array_merge($array1, $array2);
echo "----------------array_merge---------------".PHP_EOL;
print_r($result);
echo "----------------+++++++++++---------------".PHP_EOL;
print_r($array1+$array2);
echo "----------------array_merge_recursive---------------".PHP_EOL;
print_r(array_merge_recursive($array1,$array2));
----------------array_merge---------------
Array
(
[0] => a
[1] => b
[2] => 1
[3] => 2
)
----------------+++++++++++---------------
Array
(
[0] => a
[1] => b
)
----------------array_merge_recursive---------------
Array
(
[0] => a
[1] => b
[2] => 1
[3] => 2
)
边栏推荐
猜你喜欢
随机推荐
接口流量突增,如何做好性能优化?
matplotlib中的3D绘图警告解决:MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure
并发模型和I/O模型介绍
2022 Shandong International Youth Eye Health Industry Exhibition, Vision Health Exhibition, Optometry Exhibition
流程控制for和while循环语句
49. 字母异位词分组-排序法
2022年8月2日——使用idea搭建servlet+jsp项目
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
async-await
【Autosar RTM】
【飞控开发高级教程2】疯壳·开源编队无人机-遥控整机代码走读、编译与烧写
Rasa 3.x study series - Rasa - Issues 4792 socket debug logs clog up debug feed study notes
【图像分类】2021-EfficientNetV2 CVPR
7.31
电压传感器: 工作原理、类型及电路图
4、Citrix MCS云桌面无法安装todesk等软件
SAP 电商云 Spartacus UI 的持续集成 - Continous integration
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
vant-swipe adaptive picture height + picture preview
如何修复 SAP UI5 aggregation with cardinality 0..1 相关的错误消息