Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




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


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




<?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);
?>


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




<?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);
?>


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" 
            } 
        } 
    }

 



Last Updated : 08 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads