PHP合并数组我们可以使用array_merge()函数,array_merge()函数返回一个联合的数组。所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次追加。其形式为:

array array_merge (array array1 array2…,arrayN)

下面是一个PHP合并数组的例子:

<?php
$fruits = array("apple","banana","pear");
$numbered = array("1","2","3");
$cards = array_merge($fruits, $numbered);
print_r($cards);
// 输出结果:
// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 )
?>

用PHP追加数组,使用array_merge_recursive(),将两个数组合并在一起,注意,与array_merge()函数是不一样的,array_merge()的两个数组有重复项时会覆盖掉,而array_merge_recursive()则不会。array_merge_recursive()语法:

array array_merge_recursive(array array1,array array2[…,array arrayN])

下面是一个PHP追加数组的例子:

<?php
$fruit1 = array("apple" => "red", "banana" => "yellow");
$fruit2 = array("pear" => "yellow", "apple" => "green");
$result = array_merge_recursive($fruit1, $fruit2);
print_r($result);
// 输出结果:
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow )
?>

现在apple 指向一个数组,由两个颜色值组成的索引数组。

PHP 合并数组 追加数组例子的更多相关文章

  1. JS数组追加数组采用push.apply的坑

    JS数组追加数组没有现成的函数,这么多年我已经习惯了a.push.apply(a, b);这种自以为很酷的,不需要写for循环的写法,一直也没遇到什么问题,直到今天我要append的b是个很大的数组时 ...

  2. JS数组追加数组采用push.apply的坑(转)

    JS数组追加数组没有现成的函数,这么多年我已经习惯了a.push.apply(a, b);这种自以为很酷的,不需要写for循环的写法,一直也没遇到什么问题,直到今天我要append的b是个很大的数组时 ...

  3. JS数组追加数组採用push.apply的坑

    JS数组追加数组没有现成的函数,这么多年我已经习惯了a.push.apply(a, b);这样的自以为非常酷的,不须要写for循环的写法,一直也没遇到什么问题,直到今天我要append的b是个非常大的 ...

  4. php 操作数组 (合并,拆分,追加,查找,删除等)

    1. 合并数组 array_merge()函数将数组合并到一起,返回一个联合的数组.所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加.其形式为: array array_merg ...

  5. php追加数组

    <?php //追加数组 array_merge_recursive()函数与array_merge()相同,可以将两个或多个数组合并在一起,形成一个联合的数组.两者之间的区别在于,当某个输入数 ...

  6. ruby中数组的常用方法----例子

    #初始化 a = Array.new p a #=>[] a = Array.new(5) p a #=>[nil, nil, nil, nil, nil] a = Array.new(5 ...

  7. JS合并两个数组的方法

    JS合并两个数组的方法 我们在项目过程中,有时候会遇到需要将两个数组合并成为一个的情况.比如: var a = [1,2,3]; var b = [4,5,6]; 有两个数组a.b,需求是将两个数组合 ...

  8. python将两个数组合并成一个数组的两种方法的代码

    内容过程中,把写内容过程中常用的内容收藏起来,下面的资料是关于python将两个数组合并成一个数组的两种方法的内容,希望能对小伙伴们有帮助. c1 = ["Red","G ...

  9. java学习之—合并两个数组并排序

    /** * 合并两个数组并排序 * Create by Administrator * 2018/6/26 0026 * 下午 4:29 **/ public class MergeApp { pub ...

随机推荐

  1. mac编译PHP报错 configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/

    解决办法 brew install curl xcode-select --install

  2. Allocation Failure

    up vote 8 down vote accepted "Allocation Failure" is a cause of GC cycle to kick. "Al ...

  3. 转 区别 getChildFragmentManager getSupportFragmentManager

    The definition of getChildFragmentManager() is: Return a private FragmentManager for placing and man ...

  4. ural1097 Square Country 2

    Square Country 2 Time limit: 1.0 secondMemory limit: 64 MB The Square Parliament of the Square count ...

  5. opencv-jni -调试出错taking address of temporary [-fpermissive]

    今天在进行代码往安卓平台移植时,IplImage *qImg=&(IplImage)dst1;报错taking address of temporary [-fpermissive] 百度了一 ...

  6. 网络通信TCP编程实例代码

    Makefile: all: gcc -o server server.c -lpthread gcc -o client client.c clean: rm server client serve ...

  7. css---使用class和id

    网页现在的新标准是W3C.目前的模式是html+css+javascript,如何理解呢,就是html是网页的结构,CSS是网页的样式,javascript是行为.结构就是盖房子先要把结构建出来,然后 ...

  8. Eclipse开发工具的使用之-使用Eclipse的Debug调试Android程序

    1.设置断点,双击Eclipse编辑界面的边界,或者右击编辑界面的边界,快捷键Ctrl+Shift+B. 2.F11键开始调试程序,程序安装到手机之后,并不会自动运行,需要你手动运行到断点处. 3.运 ...

  9. MVC3在页面上获取当前控制器名称、Action名称以及路由参数

    获取控制器名称: ViewContext.RouteData.Values["controller"].ToString(); 获取Action名称: ViewContext.Ro ...

  10. CodeForces 610B Vika and Squares

    #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...