How to combine 2 associative arrays in php such that we do not overwrite any duplicate entries in all cases?

前端 未结 2 1797
醉梦人生
醉梦人生 2020-12-31 12:06

I have two associative array which have many content same and so I want to combine those two arrays in such a way that if I have a in array 1 and a

2条回答
  •  有刺的猬
    2020-12-31 12:26

    array_merge() will override duplicates only if both arrays contain the same string keys:

    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.

    I believe this behavior is expected. How would you handle this case? Let's say that

    $arr1 = array('a' => 1);
    $arr2 = array('a' => 2);
    

    What would be an acceptable output after the array merge?

提交回复
热议问题