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

08 Jun, 2020
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" 
            } 
        } 
    }


 
PHP-array
Picked
PHP
Web Technologies
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to fetch data from localserver database and display on HTML table using PHP ?
Difference between HTTP GET and POST Methods
Different ways for passing data to view in Laravel
PHP | file_exists( ) Function
PHP | Ternary Operator
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?