Uffo Uffo - 3 years ago 77
PHP Question

Array merge with same key's dont' overwrite

So I have the following 2 arrays, with keys

2016
and
2017


array(2) {
[2016]=>
array(11) {
["id"]=>
string(1) "0"
[0]=>
string(1) "0"
["user_id"]=>
string(1) "0"
["entry_id"]=>
int(0)
["user_name"]=>
string(0) ""
[3]=>
string(0) ""
["action"]=>
string(11) "banner view"
[4]=>
string(11) "banner view"
["entry_details"]=>
string(0) ""
["created"]=>
string(4) "2016"
["total"]=>
int(0)
}
[2017]=>
array(11) {
["id"]=>
string(1) "0"
[0]=>
string(1) "0"
["user_id"]=>
string(1) "0"
["entry_id"]=>
int(0)
["user_name"]=>
string(0) ""
[3]=>
string(0) ""
["action"]=>
string(11) "banner view"
[4]=>
string(11) "banner view"
["entry_details"]=>
string(0) ""
["created"]=>
string(4) "2017"
["total"]=>
int(0)
}
}
array(1) {
[2017]=>
array(30) {
["id"]=>
string(2) "52"
[0]=>
string(2) "52"
["user_id"]=>
NULL
[1]=>
NULL
["entry_id"]=>
string(1) "2"
[2]=>
string(1) "2"
["user_name"]=>
NULL
[3]=>
NULL
["action"]=>
string(11) "banner view"
[4]=>
string(11) "banner view"
["entry_details"]=>
string(4) "null"
[5]=>
string(4) "null"
["entry_type"]=>
string(6) "banner"
[6]=>
string(6) "banner"
["created"]=>
string(19) "2017-06-21 10:25:49"
[7]=>
string(19) "2017-06-21 10:25:49"
["ip"]=>
string(9) "127.0.0.1"
[8]=>
string(9) "127.0.0.1"
["user_agent"]=>
string(121) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
[9]=>
string(121) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
["browser"]=>
string(13) "Google Chrome"
[10]=>
string(13) "Google Chrome"
["browser_version"]=>
string(2) "58"
[11]=>
string(2) "58"
["os"]=>
string(28) "Mac OSX 10 (Unknown Version)"
[12]=>
string(28) "Mac OSX 10 (Unknown Version)"
["total"]=>
string(3) "510"
[13]=>
string(3) "510"
["date_only"]=>
string(4) "2017"
[14]=>
string(4) "2017"
}
}


Now, after array merge I should get an array with 2 keys of data,
2016
and
2017
but instead I get an array with 3 keys, it's not overwriting the array with the same key.


$final = array_merge($results,$results_from_db);


array(3) {
[0]=>
array(11) {
["id"]=>
string(1) "0"
[0]=>
string(1) "0"
["user_id"]=>
string(1) "0"
["entry_id"]=>
int(0)
["user_name"]=>
string(0) ""
[3]=>
string(0) ""
["action"]=>
string(11) "banner view"
[4]=>
string(11) "banner view"
["entry_details"]=>
string(0) ""
["created"]=>
string(4) "2016"
["total"]=>
int(0)
}
[1]=>
array(11) {
["id"]=>
string(1) "0"
[0]=>
string(1) "0"
["user_id"]=>
string(1) "0"
["entry_id"]=>
int(0)
["user_name"]=>
string(0) ""
[3]=>
string(0) ""
["action"]=>
string(11) "banner view"
[4]=>
string(11) "banner view"
["entry_details"]=>
string(0) ""
["created"]=>
string(4) "2017"
["total"]=>
int(0)
}
[2]=>
array(30) {
["id"]=>
string(2) "52"
[0]=>
string(2) "52"
["user_id"]=>
NULL
[1]=>
NULL
["entry_id"]=>
string(1) "2"
[2]=>
string(1) "2"
["user_name"]=>
NULL
[3]=>
NULL
["action"]=>
string(11) "banner view"
[4]=>
string(11) "banner view"
["entry_details"]=>
string(4) "null"
[5]=>
string(4) "null"
["entry_type"]=>
string(6) "banner"
[6]=>
string(6) "banner"
["created"]=>
string(19) "2017-06-21 10:25:49"
[7]=>
string(19) "2017-06-21 10:25:49"
["ip"]=>
string(9) "127.0.0.1"
[8]=>
string(9) "127.0.0.1"
["user_agent"]=>
string(121) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
[9]=>
string(121) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
["browser"]=>
string(13) "Google Chrome"
[10]=>
string(13) "Google Chrome"
["browser_version"]=>
string(2) "58"
[11]=>
string(2) "58"
["os"]=>
string(28) "Mac OSX 10 (Unknown Version)"
[12]=>
string(28) "Mac OSX 10 (Unknown Version)"
["total"]=>
string(3) "510"
[13]=>
string(3) "510"
["date_only"]=>
string(4) "2017"
[14]=>
string(4) "2017"
}
}


Why is this happening? Is it normal?


PHP 5.6.30 (cli) (built: Feb 7 2017 16:18:37) Copyright (c)
1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016
Zend Technologies

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download