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

前端 未结 2 1795
醉梦人生
醉梦人生 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:11

    $array1 + $array2 will return the union of the two arrays. If they are associative, values from the left operand will be preferred if keys collide. Therefore, this is not the solution you want as values will be lost.

    There is nothing wrong with the statement provided both variables are understood as arrays. Your fatal error likely occurred because one variable was actually an object, and it's possible to mistake that using var_dump. By adding the type casts you forced PHP to coerce both variables to arrays. If one variable was an object, it would have this effect:

    From the PHP.net Manual

    "If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side."

    Now having two arrays to add, PHP had nothing to panic about. Note that while casting the two operands of + was important, it was meaningless to cast the variable you were assigning to. The expression (array)$foo does not modify $foo, but rather returns a new value. This may not be intuitive from languages that require you to declare variables, but PHP is not such a language.

    On your other issue, you cannot have two of the same key in an array. This would make it impossible to index the array as the correct value to return would become ambiguous. If you wish to combine two arrays in a lossless manor, you will have to use a more complicated data structure.

    My suggestion is to use an array of arrays, where:

    $a1 = array('a' => 1, 'b' => 2, 'c' => 3);
    $a2 = array('a' => 3, 'b' => 2, 'd' => 2);
    

    Will become:

    $a3 = array(
        'a' => array(1, 3),
        'b' => array(2, 2),
        'c' => array(3),
        'd' => array(2)
    );
    

    And the ordering is not determined. The only notable change to the structure is that all first values are arrays, allowing values of duplicate keys to be accumulated. This function does the task and might do with a better name:

    // array(array) lossless_array_merge([$array1 [, $array2 [, $...]]])
    
    function lossless_array_merge() {
      $arrays = func_get_args();
      $data = array();
      foreach ($arrays as $a) {
        foreach ($a as $k => $v) {
          $data[$k][] = $v;
        }
      }
      return $data;
    }
    
    0 讨论(0)
  • 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?

    0 讨论(0)
提交回复
热议问题