7

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Chayan Biswas
  • 566
  • 2
  • 6
  • 18

4 Answers4

9

You can refer to the PHP manual, but for now I will explain the difference like this:

array_push()

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

Example

$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

$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
)
BadHorsie
  • 14,135
  • 30
  • 117
  • 191
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
3

array_push() adds each argument to the array:

array_push($array1, $array2);  
//is the same as  
$array1[] = $array2;

This will create a multidimensional array, which I don't think is what you want. Array merge just puts them together to make one big array, which I think is what you're after. To get the same effect with array_push(), you'd have to do this:

//assuming $array2 has 4 elements 
array_push($array1, $array2[0], $array2[1], $array2[2], $array2[3]); 
//is the same as 
$array1 = array_merge($array1, $array2); 
//is the same as 
$array1[] = $array2[0]; 
$array1[] = $array2[1]; 
$array1[] = $array2[2]; 
$array1[] = $array2[3];
Ramirez
  • 124
  • 1
  • 1
  • 6
2

array_push(&$arr, $mixed [, $mixed]):int (number of elements)

pushes one or more elements onto an array as if it were a stack.

so.

$arr = ['1','2','3'];
array_push($arr, '4');

becomes

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

array_merge($arr1, $arr2 [, array $... ] ):array

$arr1 = ['1','2','3'];
$arr2 = ['4','5','6'];

$arr3 = array_merge($arr1, $arr2);

Note the 3rd array.

becomes

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)

Both functions can take multiple parameters.

DevDonkey
  • 4,835
  • 2
  • 27
  • 41
1

array_push — Push one or more elements onto the end of array It will not use two different arrays just we have one array and in array_merge we have two different arrays and we merge them into one array

<?php
$stack = array("foo", "bar");
array_push($stack, "bar2", "foo2");
print_r($stack);
?>

outpur will be

Array
(
    [0] => foo
    [1] => bar
    [2] => bar2
    [3] => foo2
)

while the array_marge combine two different arrays

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);

output will be

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)
Mirza Obaid
  • 1,707
  • 3
  • 23
  • 35