美文网首页
php array_merge、+、array_merge_r

php array_merge、+、array_merge_r

作者: ZaccurWang | 来源:发表于2018-08-21 15:05 被阅读0次

    概念不赘述,先看代码

    <?php
        $arrayOne = [
              1     => 'a',
              'one' => 'b',
              2     => 'c',
              'two' => 'd'
        ];
    
        $arrayTwo = [
              0       => 'e',
              'one'   => 'f',
              1       => 'g',
              'three' => 'h'
        ];
        
        $merge      = array_merge($arrayOne, $arrayTwo);
    
        $plus       = $arrayOne + $arrayTwo;
    
        $recursive  = array_merge_recursive($arrayOne, $arrayTwo);
    
        print_r($merge);
        print_r($plus);
        print_r($recursive);
    

    运行结果

    Array
    (
       [0] => a
       [one] => f
       [1] => c
       [two] => d
       [2] => e
       [3] => g
       [three] => h
    )
    Array
    (
       [1] => a
       [one] => b
       [2] => c
       [two] => d
       [0] => e
       [three] => h
    )
    Array
    (
       [0] => a
       [one] => Array
           (
               [0] => b
               [1] => f
           )
       [1] => c
       [two] => d
       [2] => e
       [3] => g
       [three] => h
    )
    

    概述一下,
    array_merge。数字键,两数组均保留并从0开始重新排列键值;字符串键,键名不同时均保留,键名相同时保留第二个数组中值。
    +。无论数字键名或字符串键名,键名不同时均保留,键名相同时保留第一个数组中值。
    array_merge_recursive。数字键,两数组均保留并从0开始重新排列键值;字符串键,键名不同时均保留,键名相同时生成二维数组保留两数组中出现的值。

    相关文章

      网友评论

          本文标题:php array_merge、+、array_merge_r

          本文链接:https://www.haomeiwen.com/subject/optjiftx.html