2

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?

Info: 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

2
  • Read the manual: "If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended." php.net/manual/en/function.array-merge.php Commented Jul 25, 2017 at 12:00
  • What is your expected result array? Which array is $results, and which is $results_from_db? Commented Jul 25, 2017 at 12:11

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Do you know a solution that could apply in my case?
I think array_replace will be a better solution in this case, thank you for helping me understand
2

array_replace() will overwrite your first 2017 subarray with your second:

var_export(array_replace($a,$b));


+ operator, then ksort() will do the same:

$overwrite=$b+$a;
ksort($overwrite);
var_export($overwrite);


A demo link where you can play with functions on the two array variables.

array_merge() and array_merge_recursive() will result in 3 subarrays.


Even if you convert the keys to strings like '2017', array_merge() will still treat them like numbers, and you'll get 3 subarrays.

If you go as far as prefix each key with #, then you are able to use array_merge() as intended. Try with #[year] on all keys and you will have 2 merged subarrays. (you can test for yourself in the above demo link)

1 Comment

This should be the best answer because not only it explains better the concept of how to handle the proposed issue, but also it brings valuable knowledge to the context while including a sandbox testing environment. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.