• PHP中array_merge函数与array+array的区别


    在PHP中可以使用array_merge函数和两个数组相加array+array的方式进行数组合并,但两者效果并不相同,下面为大家介绍两者具体的使用区别.

    区别如下:

     当下标为数值时,array_merge()不会覆盖掉原来的值,但array+array合并数组则会把最先出现的值作为最终结果返回,而把后面的数组拥有相同键名的那些值“抛弃”掉(不是覆盖). 

    当下标为字符时,array+array仍然把最先出现的值作为最终结果返回,而把后面的数组拥有相同键名的那些值“抛弃”掉,但array_merge()此时会覆盖掉前面相同键名的值. 

    例子1:

    代码:

    $arr1 = ['PHP', 'apache'];
    $arr2 = ['PHP', 'MySQl', 'HTML', 'CSS'];
    $mergeArr = array_merge($arr1, $arr2);
    $plusArr = $arr1 + $arr2;
    var_dump($mergeArr);
    var_dump($plusArr);

    结果
    $mergeArr:

    array (size=6)
      0 => string 'PHP' (length=3)
      1 => string 'apache' (length=5)
      2 => string 'PHP' (length=3)
      3 => string 'MySQl' (length=5)
      4 => string 'HTML' (length=4)
      5 => string 'CSS' (length=3)

    $plusArr

    array (size=4)
      0 => string 'PHP' (length=3)
      1 => string 'apache' (length=5)
      2 => string 'HTML' (length=4)
      3 => string 'CSS' (length=3

    例子2:

    代码:

    $arr1 = ['PHP', 'a'=>'MySQl'];
    $arr2 = ['PHP', 'MySQl', 'a'=>'HTML', 'CSS'];
    $mergeArr = array_merge($arr1, $arr2);
    $plusArr = $arr1 + $arr2;
    var_dump($mergeArr);
    var_dump($plusArr);

    结果
    $mergeArr:

    array (size=5)
      0 => string 'PHP' (length=3)
      'a' => string 'HTML' (length=4)
      1 => string 'PHP' (length=3)
      2 => string 'MySQl' (length=5)
      3 => string 'CSS' (length=3)

    $plusArr

    array (size=4)
      0 => string 'PHP' (length=3)
      'a' => string 'MySQl' (length=5)
      1 => string 'MySQl' (length=5)
      2 => string 'CSS' (length=3)

    例子3:

    代码:

    $arr1 = ['PHP', 'a'=>'MySQl','6'=>'CSS'];
    $arr2 = ['PHP', 'MySQl', 'a'=>'HTML', 'CSS'];
    $mergeArr = array_merge($arr1, $arr2);
    $plusArr = $arr1 + $arr2;
    var_dump($mergeArr);
    var_dump($plusArr);

    结果:

    $mergeArr

    array (size=6)
      0 => string 'PHP' (length=3)
      'a' => string 'HTML' (length=4)
      1 => string 'CSS' (length=3)
      2 => string 'PHP' (length=3)
      3 => string 'MySQl' (length=5)
      4 => string 'CSS' (length=3)

    $plusArr

    array (size=5)
      0 => string 'PHP' (length=3)
      'a' => string 'MySQl' (length=5)
      6 => string 'CSS' (length=3)
      1 => string 'MySQl' (length=5)
      2 => string 'CSS' (length=3)

  • 相关阅读:
    hdfs fsck命令查看HDFS文件对应的文件块信息(Block)和位置信息(Locations)
    更高的压缩比,更好的性能–使用ORC文件格式优化Hive
    InfluxDB基本概念和操作
    InfluxDB部署
    Zookeeper运维小结--CancelledKeyException
    Zookeeper源码编译为Eclipse工程(win7下Ant编译)
    ZooKeeper Observers解决节点过多时写性能下降问题
    ZooKeeper日志与快照文件简单分析
    ZooKeeper Administrator's Guide A Guide to Deployment and Administration(吃别人嚼过的馍没意思,直接看官网资料)
    ZOOKEEPER解惑
  • 原文地址:https://www.cnblogs.com/yixiao21/p/8400561.html
Copyright © 2020-2023  润新知