0

I have two arrays,

Array 1(
  'A' => string '13' (length=2)
  'B' => string '13' (length=2)
)

Array 2(
   9 => string '13' (length=2)
)

But when I use array_merge to combine these arrays, it shows me like this

Array(
   'A' => string '13' (length=2)
   'B' => string '13' (length=2) 
    0 => string '13' (length=2)
)

Everytime when there is numeric value, in array_merge it increments automatically, not taking original value.

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
Dev
  • 489
  • 5
  • 14
  • As per the docs: http://php.net/array_merge `... will be renumbered with incrementing keys starting from zero in the result array.` – Marc B Mar 20 '13 at 06:05

2 Answers2

6
<?php
$a = array('A' => 13, 'B' => 13);
$b = array('9' => 13);

print_r($a+$b);
?>
Dead Man
  • 2,880
  • 23
  • 37
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33
0

array_merge() will do the reindexing on the numeric indexes.. You you just want to append one array to other without and reindexing or shuffling use "+" then ...as shown below

<?php
$array1 = array('a'=>'13','b'=>'14');
$array2 = array(9=>'13');
$merged_array  = $array1+$array2;
print_r($merged_array);
?>

Documentation link http://php.net/manual/en/function.array-merge.php

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67