13

How can i do an array_merge on an associative array, like so:

Array 1:

$options = array (
"1567" => "test",
"1853" => "test1",
);

Array 2:

$option = array (
"none" => "N/A"
);

So i need to array_merge these two but when i do i get this (in debug):

Array
(
    [none] => N/A
    [0] => test
    [1] => test1
)
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
  • _"[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)"_. If you can change your `$options` array keys to contain some string, then [it will work just fine](http://codepad.org/d5DbFdas). – Shef Jul 25 '11 at 09:19
  • @Shef: You should mention: Non-Numeric strings. – hakre Jul 25 '11 at 10:33

7 Answers7

14

try using :

$finalArray = $options + $option .see http://codepad.org/BJ0HVtac Just check the behaviour for duplicate keys, I did not test this. For unique keys, it works great.

<?php

$options = array (
                  "1567" => "test",
                  "1853" => "test1",
                  );


$option = array (
"none" => "N/A"
);


$final = array_merge($option,$options);

var_dump($final);


$finalNew = $option + $options ;

var_dump($finalNew);

?>
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • 2
    Also remember that after merge the keys will be "0" and "1". array_merge() function resets numeric keys! – ram4nd Mar 02 '17 at 14:47
11

Just use $options + $option!

var_dump($options + $option);

outputs:

array(3) {
  [1567]=>
  string(4) "test"
  [1853]=>
  string(5) "test1"
  ["none"]=>
  string(3) "N/A"
}

But be careful when there is a key collision. Here is what the PHP manual says:

The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

Tomas
  • 57,621
  • 49
  • 238
  • 373
2
$final_option = $option + $options;
Luke
  • 11,426
  • 43
  • 60
  • 69
Rukmi Patel
  • 2,619
  • 9
  • 29
  • 41
1

I was looking to merge two associative arrays together, adding the values together if the keys were the same. If there were keys unique to either array, these would be added into the merged array with their existing values.

I couldnt find a function to do this, so made this:

function array_merge_assoc($array1, $array2)
{
     
    if(sizeof($array1)>sizeof($array2))
    {
        echo $size = sizeof($array1);
    }
    else
    {
        $a = $array1;
        $array1 = $array2;
        $array2 = $a;
         
        echo $size = sizeof($array1);
    }
     
    $keys2 = array_keys($array2);
    
    for($i = 0;$i<$size;$i++)
    {
        $array1[$keys2[$i]] = $array1[$keys2[$i]] + $array2[$keys2[$i]];
    }
     
    $array1 = array_filter($array1);
    return $array1;
 }

Reference: http://www.php.net/manual/en/function.array-merge.php#90136

Community
  • 1
  • 1
0

This code could be used for recursive merge:

function merge($arr1, $arr2){
        $out = array();
        foreach($arr1 as $key => $val1){
            if(isset($arr2[$key])){
                if(is_array($arr1[$key]) && is_array($arr2[$key])){
                    $out[$key]=  merge($arr1[$key], $arr2[$key]);
                }else{
                    $out[$key]= array($arr1[$key], $arr2[$key]);
                }
                unset($arr2[$key]);
            }else{
                $out[$key] = $arr1[$key];
            }
        }
        return $out + $arr2;
 }
Deepak Mittal
  • 199
  • 2
  • 3
0

If arrays having same keys then use array_merge_recursive()

$array1 = array( "a" => "1" , "b" => "45" );

$array2 = array( "a" => "23" , "b" => "33" );

$newarray = array_merge_recursive($array1,$array2);

The array_merge_recursive() wont overwrite, it just makes the value as an array.

Aravindh Gopi
  • 2,083
  • 28
  • 36
0

when array_merge doesn't work, then simply do

<?php
$new = array();
foreach ($options as $key=>$value) $new[$key] = $value;
foreach ($option as $key=>$value) $new[$key] = $value;
?>

or switch the two foreach loops depending on which array has higher priority

rabudde
  • 7,498
  • 6
  • 53
  • 91