所以我有以下2個陣列,連鍵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, 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"
}
}
現在,陣列合併後,我應該得到一個數組,2個鍵數據,2016
和2017
但是我得到一個有3個鍵的數組,它不會用相同的鍵覆蓋數組。
$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"
}
}
這究竟是爲什麼?這是正常的嗎?
信息:PHP 30年6月5日(CLI)(建:2017年2月7日十六時十八分37秒)版權所有(C) 1997-2016的PHP集團Zend引擎V2.6.0,版權所有(C)1998用數字鍵輸入數組中2016 Zend技術
閱讀手冊:_「但是,如果數組中包含數字鍵,後面的值不會覆蓋原始值,但會被追加。」_ http: //php.net/manual/en/function.array-merge.php –
你的預期結果數組是什麼?哪個數組是'$ results',哪個是'$ results_from_db'? – mickmackusa