Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array_merge versus + [duplicate]

People also ask

What is array_merge?

The array_merge() function merges one or more arrays into one array. Tip: You can assign one array to the function, or as many as you like. Note: If two or more array elements have the same key, the last one overrides the others.

What is the difference between array_merge () and Array_merge_recursive () in PHP?

The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.

Does array merge remove duplicates PHP?

You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.


Because both arrays are numerically-indexed, only the values in the first array will be used.

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

http://php.net/manual/en/language.operators.array.php

array_merge() has slightly different behavior:

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. 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


These two operation are totally different.

array plus

  1. Array plus operation treats all array as assoc array.
  2. When key conflict during plus, left(previous) value will be kept
  3. null + array() will raise fatal error

array_merge()

  1. array_merge() works different with index-array and assoc-array.
  2. If both parameters are index-array, array_merge() concat index-array values.
  3. If not, the index-array will to convert to values array, and then convert to assoc array.
  4. Now it got two assoc array and merge them together, when key conflict, right(last) value will be kept.
  5. array_merge(null, array()) returns array() and got a warning said, parameter #1 is not an array.

I post the code below to make things clear.

function array_plus($a, $b){
    $results = array();
    foreach($a as $k=>$v) if(!isset($results[$k]))$results[$k] = $v;
    foreach($b as $k=>$v) if(!isset($results[$k]))$results[$k] = $v;
    return $results;
}

//----------------------------------------------------------------

function is_index($a){
    $keys = array_keys($a);
    foreach($keys as $key) {
        $i = intval($key);
        if("$key"!="$i") return false;
    }
    return true;
}

function array_merge($a, $b){
    if(is_index($a)) $a = array_values($a);
    if(is_index($b)) $b = array_values($b);
    $results = array();
    if(is_index($a) and is_index($b)){
        foreach($a as $v) $results[] = $v;
        foreach($b as $v) $results[] = $v;
    }
    else{
        foreach($a as $k=>$v) $results[$k] = $v;
        foreach($b as $k=>$v) $results[$k] = $v;
    }
    return $results;
}