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'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.
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
)
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
)
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.
Related Topics
Insert Query Check If Record Exists - If Not, Insert It
Cast the Current Object ($This) to a Descendent Class
How to Put JavaScript Variable in PHP Echo
Difference Between Var_Dump,Var_Export & Print_R
How to Handle User Input of Invalid Utf-8 Characters
Design Patterns: How to Create Database Object/Connection Only When Needed
Fatal Error: Out of Memory, But I Do Have Plenty of Memory (Php)
Unit Test Laravel's Formrequest
PHP Get Both Array Value and Array Key
Using Variables in MySQL Update (Php/Mysql)
What Causes an Http 405 "Invalid Method (Http Verb)" Error When Posting a Form to PHP on Iis
How to Create Codeigniter Language Files from Database
Fatal Error - 'Mongo' Class Not Found
Check If Url Has Certain String with PHP