3

I have two array and I want to merge those array into one array each. How can I accomplish this code and what I mistake in here:

Bellow is my code:

$intro = array( array( "name"=>"Peter","age"=>"40","location"=>"USA" ), 
            array("name"=>"Mike","age"=>"55","location"=>"USA")
);

$bikes = array( array( "brand"=>"Hero","cc"=>"150", "rpm"=>"8500"),
            array( "brand"=>"Honda","cc"=>"150", "rpm"=>"9500")
);

$result = array_merge($intro, $bikes);

print_r($result);

After merging those two arrays I am getting this result:

Array
(
    [0] => Array
        (
            [name] => Peter
            [age] => 40
            [location] => USA
        )

    [1] => Array
        (
            [name] => Mike
            [age] => 55
            [location] => USA
        )

    [2] => Array
        (
            [brand] => Hero
            [cc] => 150
            [rpm] => 8500
        )

    [3] => Array
        (
            [brand] => Honda
            [cc] => 150
            [rpm] => 9500
        )

)

But I want to get below pattern:

Array
(
    [0] => Array
        (
            [name] => Peter
            [age] => 40
            [location] => USA
            [brand] => Hero
            [cc] => 150
            [rpm] => 8500
        )
     [1] => Array
        (
            [name] => Mike
            [age] => 55
            [location] => USA
            [brand] => Yamaha
            [cc] => 150
            [rpm] => 9500
            
        )
    

)

Any help from the expert will highly appreciated.

Thanks

Kevin
  • 41,694
  • 12
  • 53
  • 70
  • 1
    you can do that with a `foreach ($array_expression as $key => $value) {` then you have the key, and can use that key to get the entry from the other array, and push that into the current $value array. – Stender Sep 08 '20 at 07:06

3 Answers3

4

If you want them merged and lined up using the keys, you could simply use good ol' foreach:

foreach ($intro as $k => $v) {
    $result[] = array_merge($v, $bikes[$k]);
}

Or a one-liner using array_map:

$result = array_map('array_merge', $intro, $bikes);
Kevin
  • 41,694
  • 12
  • 53
  • 70
1

array_map with a custom callback can help:

$intro = array( 
    array( "name"=>"Peter","age"=>"40","location"=>"USA" ), 
    array("name"=>"Mike","age"=>"55","location"=>"USA")
);

$bikes = array( 
    array( "brand"=>"Hero","cc"=>"150", "rpm"=>"8500"),
    array( "brand"=>"Honda","cc"=>"150", "rpm"=>"9500")
);

$result = array_map(function($a, $b) {
    // here you merge every subarray from 
    // `$intro` with subarray from `$bikes`
    return array_merge($a, $b);
}, $intro, $bikes);

print_r($result);

Fiddle here.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

You can do this :

foreach ($intro as $k => $v) {
    $result[] = array_merge($v, $bikes[$k]);
}

result :

Array
(
[0] => Array
    (
        [name] => Peter
        [age] => 40
        [location] => USA
        [brand] => Hero
        [cc] => 150
        [rpm] => 8500
    )

[1] => Array
    (
        [name] => Mike
        [age] => 55
        [location] => USA
        [brand] => Honda
        [cc] => 150
        [rpm] => 9500
    )
)
Skyd
  • 535
  • 1
  • 4
  • 20