-6

I have been trying to merge two associative arrays all day. But I don't know whether it is possible or not in PHP.

first array

  'images' => 
      0 => 
          'image_first' => string 'http://10.0.2.2/captcha-2/iphone.png'
      1 => 
          'image_first' => string 'http://10.0.2.2/captcha-2/1425328_405486846250283_806381377_o.jpg'
      2 => 
          'image_first' => string 'http://10.0.2.2/captcha-2/bbg.jpg'
      3 => 
          'image_first' => string 'http://10.0.2.2/captcha-2/gmail_bg.JPG'

second array

'images' => 
      0 => 
          'image_second' => string 'http://10.0.2.2/captcha-2/Capture.JPG'
      1 => 
          'image_second' => string 'http://10.0.2.2/captcha-2/bg.jpg'
      2 => 
          'image_second' => string 'http://10.0.2.2/captcha-2/abc.JPG'
      3 => 
          'image_second' => string 'http://10.0.2.2/captcha-2/bg_dark.png'
      4 => 
          'image_second' => string 'http://10.0.2.2/captcha-2/bktile.png'

And they should be merged together like this.

merged array

  'images' => 
      0 => 
          'image_first' => string 'http://10.0.2.2/captcha-2/iphone.png'
      1 => 
          'image_first' => string 'http://10.0.2.2/captcha-2/1425328_405486846250283_806381377_o.jpg'
      2 => 
          'image_first' => string 'http://10.0.2.2/captcha-2/bbg.jpg'
      3 => 
          'image_first' => string 'http://10.0.2.2/captcha-2/gmail_bg.JPG'
      4 => 
          'image_second' => string 'http://10.0.2.2/captcha-2/Capture.JPG'
      5 => 
          'image_second' => string 'http://10.0.2.2/captcha-2/bg.jpg'
      6 => 
          'image_second' => string 'http://10.0.2.2/captcha-2/abc.JPG'
      7 => 
          'image_second' => string 'http://10.0.2.2/captcha-2/bg_dark.png'
      8 => 
          'image_second' => string 'http://10.0.2.2/captcha-2/bktile.png'

How to do it?

Hybrid Developer
  • 2,320
  • 1
  • 34
  • 55

1 Answers1

4

array_merge() will do the trick. See http://de2.php.net/array_merge.

theHacker
  • 3,984
  • 1
  • 19
  • 34
  • -1 because: from the docs 'If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. 'see @Fydo's answer – toesslab May 13 '14 at 17:00
  • @pc-shooter Literally the next sentence in the documentation: "If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended." – Alexis King May 13 '14 at 17:04
  • @pc-shooter -1 your comment, because when the array has numeric keys, they are not replaced. See @ php documentation's answer. – Chris Baker May 13 '14 at 17:04
  • 1
    @theHacker ooops!! sry for that!! I can't vote up again, until you edit your answer... But I will, as soon as I can!! – toesslab May 13 '14 at 17:05
  • @pc-shooter did edit, hope that helps :-) – theHacker May 13 '14 at 21:50
  • @theHacker +1 sry again! – toesslab May 14 '14 at 03:58