How to merge the first index of an array with the first index of second array?

The task is to merge the first index of an array with the first index of another array. Suppose, an array is array1 = {a, b, c} and another array is array2 = {c, d, e} if we perform the task on these arrays then the output will be 

result array
    { 
    [0]=> array(2) 
        { 
        [0]=> string(1) "a" 
        [1]=> string(1) "c" 
        } 
    [1]=> array(2) 
        { 
        [0]=> string(1) "b" 
        [1]=> string(1) "d" 
        } 
    [2]=> array(2) 
            { 
            [0]=> string(1) "c" 
            [1]=> string(1) "e" 
            } 
    }

Most people think array_merge() function can resolve the above requirement the following code shows how this isn’t the way to achieve it: 

Example 1: Using the array_merge() function will give you the desired result.

php

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $array1=array("a","b","c");
    $array2=array("c","d","e");
    $result=array_merge($array1,$array2);
    var_dump($result);
?>

chevron_right


Output: 



array(6) { [0]=> string(1) "a" 
           [1]=> string(1) "b" 
           [2]=> string(1) "c" 
           [3]=> string(1) "c" 
           [4]=> string(1) "d" 
           [5]=> string(1) "e" 
         }

In order to combine the two arrays by indices, we have to loop through them and merge as we go. Such that the first index of the first and second array together form the first index of the resultant array.

Example 2: Program to merge 2 simple arrays 

php

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $array1=array("a","b","c");
    $array2=array("c","d","e");
    $result=array();
    foreach($array1 as $key=>$value ){
      $val=$array2[$key];
      $result[$key]=array($value,$val);
    }
  
    var_dump($result);
?>

chevron_right


Output: 

array(3) { 
    [0]=> array(2) 
        { 
        [0]=> string(1) "a" 
        [1]=> string(1) "c" 
        } 
    [1]=> array(2) 
        { 
        [0]=> string(1) "b" 
        [1]=> string(1) "d" 
        } 
    [2]=> array(2) 
            { 
            [0]=> string(1) "c" 
            [1]=> string(1) "e" 
            } 
    }

Example 3: Program to merge 2 complex arrays 

php

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $array1=array(array("a","b"),array("c","d"));
    $array2=array(array("z","y"),array("x","w"));
    $result=array();
    foreach($array1 as $key=>$value ){
      $val=$array2[$key];
      $result[$key]=array($value,$val);
    }
  
    var_dump($result);
?>

chevron_right


Output: 

array(2) { 
    [0]=> array(2) 
        { 
        [0]=> array(2) 
            { 
            [0]=> string(1) "a" 
            [1]=> string(1) "b" 
            } 
        [1]=> array(2) 
            { 
            [0]=> string(1) "z" 
            [1]=> string(1) "y" 
            } 
        } 
    [1]=> array(2) 
        {
        [0]=> array(2) 
            { 
            [0]=> string(1) "c" 
            [1]=> string(1) "d" 
            } 
        [1]=> array(2) 
            { 
            [0]=> string(1) "x" 
            [1]=> string(1) "w" 
            } 
        } 
    }

 




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.