What's the Difference Between Array_Merge and Array + Array

What's the difference between array_merge and array + array?

The difference is:

The + operator takes the union of the two arrays, whereas the array_merge function takes the union BUT the duplicate keys are overwritten.

Array_merge versus +

Because both arrays are numerically-indexed, only the values in the first array will be used.

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

http://php.net/manual/en/language.operators.array.php

array_merge() has slightly different behavior:

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.

http://php.net/manual/en/function.array-merge.php

What is the difference between array_push() and array_merge()?

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
)

Differences between array_replace and array_merge in PHP

For arrays with string keys, yes these are equivalent, as you mentioned. If you have numeric keys, array_merge() will append them as required, and even re-order them if necessary, whereas array_replace() will overwrite the original values.

For example,

$a = array('a' => 'hello', 'b' => 'world');
$b = array('a' => 'person', 'b' => 'thing', 'c'=>'other', '15'=>'x');

print_r(array_merge($a, $b));
/*Array
(
[a] => person
[b] => thing
[c] => other
[0] => x
)*/

print_r(array_replace($a, $b));
/*Array
(
[a] => person
[b] => thing
[c] => other
[15] => x
)*/

As you can see, array_merge has re-indexed the numeric keys of the array, and both of them simply update string keys.

However, when you have numeric keys, array_merge() will simply not care about keys, and add everything in the order it sees, deleting nothing, whereas array_replace() will, as the name suggests, replace keys with similar (numeric) indices:

<?php
$a = array('0'=>'a', '1'=>'c');
$b = array('0'=>'b');

print_r(array_merge($a, $b));
/*Array
(
[0] => a
[1] => c
[2] => b
)*/

print_r(array_replace($a, $b));
/*Array
(
[0] => b
[1] => c
)*/

What is the difference between array_push() and array_merge()?

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
)

array_merge vs array_value for resetting array index

I haven't done the benchmarks either -- and if you need to be sure, you should do them.

That said, I would suspect that if one is preferable to the other, array_values() is going to be the way to go.

After all, what you want to do is exactly what array_values() was designed for.

php - array_combine or array_merge?

You can solve this is O(n) by simply iterating the arrays...

$combinedarray = array();
$len = count($field_sreference);
for ($i = 0; $i < $len; $i++) {
$combinedarray[] = array("nid" => $field_sreference[$i]['nid'],
"idelta" => $filed_idelta[$i]['value'],
"iswitch" => $field_iswitch[$i]['value']);
}

This assumes, the 3 arrays are all of equal length.

Which is faster array_merge or array_splice to merge arrays?

$arguments = ["arg1" => "111", "arg2" => "222", "arg3" => "333"];

Use array_splice = 3.273508 (in seconds)

for ($i=0; $i < 1000000; $i++) { 
$arguments = ["arg1" => "111", "arg2" => "222", "arg3" => "333"];
array_splice($arguments, 0, 0,[777]);
$arguments = [];
}

Note*
array_splice($arguments, count($arguments), 0,[777]); Same time as array_splice($arguments, 0, 0,[777]); = ~3.273508 (in seconds)

Output

Array
(
[0] => 777
[arg1] => 111
[arg2] => 222
[arg3] => 333
)

Use array_merge = 2.984012 (in seconds)

for ($i=0; $i < 1000000; $i++) { 

$arguments = ["arg1" => "111", "arg2" => "222", "arg3" => "333"];
$args = array_merge([777], $arguments);
$arguments = [];
}

Output

Array
(
[0] => 777
[arg1] => 111
[arg2] => 222
[arg3] => 333
)


Related Topics



Leave a reply



Submit