PHP array_merge key order
Asked Answered
K

2

5

Does it matter in what order the keys in an array array doing an array_merge, i.e. would the keys in the second array below override the keys in the first array:

array1 = array('username' => 'abc', 'level' => 'admin', 'status' => 'active');
array2 = array('level' => 'root', 'status' => 'active', 'username' => 'bcd');

? Or would the order of the keys have to be the same in the two arrays?

Kilan answered 25/7, 2012 at 2:22 Comment(0)
M
5

The manual states the answer to this question:

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.

So, yes the keys in the second array will overwrite the keys from the first one if the second array contains some of the same keys.

$array1 = array('username' => 'abc', 'level' => 'admin', 'status' => 'active');
$array2 = array('level' => 'root', 'status' => 'active', 'username' => 'bcd');

$new = array_merge($array1, $array2);

print_r($new);

Output:

Array
(
    [username] => bcd
    [level] => root
    [status] => active
)

So you can see that the keys from the second array overwrote the same keys from first one; the order of the keys in each array does not matter.

Meteoric answered 25/7, 2012 at 2:26 Comment(0)
C
2

Here is an alternative solution that allows the use of a template array to define the output order. Missing fields are filled with null or as specified. In this example the first and last fields are filled with ---.

Please take note of the highlighted text from the previous answer about numeric keys.

function array_merge_template($array1, $array2, $template, $fill=null) {
    $_template = array_fill_keys($template, $fill);
    return array_intersect_key ( array_replace ( $_template, array_merge($array1, $array2)) , $_template);
}

Input:

$array1 = ['username' =>'abc', 'level' =>'admin', 'status' =>'active', 'foo'=>'x'];
$array2 = ['level' =>'root', 'status' =>'active', 'username' =>'bcd', 'bar'=>'y'];
$template = ['first','level','foo','username','bar','status','last'];

Output:

/* array_merge($array1,$array2) */
[
    "username" =>  "bcd",
    "level"    =>  "root",
    "status"   =>  "active",
    "foo"      =>  "x",
    "bar"      =>  "y"
]

/* array_merge_template($array1,$array2,$template,'---') */
[
    "first"    =>  "---",
    "level"    =>  "root",
    "foo"      =>  "x",
    "username" =>  "bcd",
    "bar"      =>  "y",
    "status"   =>  "active",
    "last"     =>  "---"
]
Compeer answered 27/7, 2017 at 15:37 Comment(1)
Nicely done! I found this solution useful for dealing with JSON API data strings that changed at the source (fields dropped, or added) but needs to be inserted into a database table with prior field mappings.Hautegaronne

© 2022 - 2024 — McMap. All rights reserved.