douqiong8412 2016-03-11 09:04
浏览 137
已采纳

array_push()和array_merge()有什么区别? [关闭]

I am adding these two elements into a new array using these two functions array_push() and array_merge().

    $a1 = array();
    $a2 = 'foo';
    echo '<pre>';
    print_r( array_merge($a1, (array)$a2) );
    echo '</pre>';

And with ..

    $a1 = array();
    $a2 = 'foo';
    array_push($a1, $a2);
    echo '<pre>';
    print_r($a1);
    echo '</pre>';

all these are printing same..

Array
 (
    [0] => foo
 )

Now my question is what is the main difference between these two functions in functionality based. I am new in php, so please help me.

  • 写回答

4条回答 默认 最新

  • dongye8110 2016-03-11 09:18
    关注

    You can refer php manual, but for now i will explain you the difference like this.

    array_push() treats array as a stack, and pushes the passed variables onto the end of array . The length of array increases by the number of variables pushed. Has the same effect as:

    Example

    <?php
    $stack = array( "orange" , "banana" );
    array_push
    ($stack , "1" , "2");
    print_r ($stack );
    ?>
    The above example will output:
    Array
    (
        [0] => orange
        [1] => banana
        [2] => 1
        [3] => 2
    )
    

    Array_merge() Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array. Example

    <?php
    $array1 = array
    ("color" => "yellow" , 0, 1);
    $array2 = array
    ("a" , "b" , "color" => "blue" , "shape
    " =>"rectangle" , 1);
    $result = array_merge
    ($array1 , $array2 );
    print_r ($result );
    ?>
    

    The above example will output:

    Array
    (
        [color] => blue
        [0] => 0
        [1] => 1
        [2] => a
        [3] => b
        [shape] => rectangle
        [4] => 1
    )
    

    array_push — Push one or more elements onto the end of array

    array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed.

    array_merge — Merge one or more arrays

    Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

        <?php
        $a1 = array('foo', 'bar');
        $a2 = 'foo';
        echo '<br />';
        print_r( array_merge($a1, (array)$a2) );
        $a1 = array('foo', 'bar');
        $a2 = 'foo';
        echo '<br />';
        array_push($a1, $a2);
        print_r($a1);
        ?>
    

    As per performance array_merge is slow.There is a discussion you should read.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名