php - Array merge with same key's dont' overwrite -
so have following 2 arrays, keys 2016
, 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, 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, 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 should array 2 keys of data, 2016
, 2017
instead array 3 keys, it's not overwriting array 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, 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, 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 happening? normal?
info: php 5.6.30 (cli) (built: feb 7 2017 16:18:37) copyright (c) 1997-2016 php group zend engine v2.6.0, copyright (c) 1998-2016 zend technologies
values in input array numeric keys renumbered incrementing keys starting 0 in result array.
Comments
Post a Comment