1

Having some difficulty trying to merge two arrays with the same numeric key. I have tried array_merge() and array_merge_recursive(), but all that seems to do is append the second array.

The first array has the following form:

Array
(
    [384] => Array
        (
            [name] => SomeMovieName1
            [age] => 12.2 hrs
            [IMDBLink] => 
            [IMDBRating] => 
            [coverArt] => 
        )

    [452] => Array
        (
            [name] => SomeMovieName2
            [age] => 13.1 hrs
            [IMDBLink] => 
            [IMDBRating] => 
            [coverArt] => 
        )

    [945] => Array
        (
            [name] => SomeMovieName3
            [age] => 13.6 hrs
            [IMDBLink] => 
            [IMDBRating] => 
            [coverArt] => 
        )
)

And here is the second array I want to combine/merge with the first:

Array
(
    [384] => Array
        (
            [IMDBRating] => 7.2
            [IMDBLink] => http://www.imdb.com/LinkToMovie1
            [coverArt] => http://www.SomeLinkToCoverArt.com/1
        )

    [452] => Array
        (
            [IMDBRating] => 8.2
            [IMDBLink] => http://www.imdb.com/LinkToMovie2
            [coverArt] => http://www.SomeLinkToCoverArt.com/2
        )

    [945] => Array
        (
            [IMDBRating] => 6.2
            [IMDBLink] => http://www.imdb.com/LinkToMovie3
            [coverArt] => http://www.SomeLinkToCoverArt.com/3
        )
)

And after merging, I would like the result to be:

Array
(
    [0] => Array
        (
            [name] => SomeMovieName1
            [age] => 12.2 hrs
            [IMDBRating] => 7.2
            [IMDBLink] => http://www.imdb.com/LinkToMovie1
            [coverArt] => http://www.SomeLinkToCoverArt.com/1
        )

    [1] => Array
        (
            [name] => SomeMovieName2
            [age] => 13.1 hrs
            [IMDBRating] => 8.2
            [IMDBLink] => http://www.imdb.com/LinkToMovie2
            [coverArt] => http://www.SomeLinkToCoverArt.com/2
        )

    [2] => Array
        (
            [name] => SomeMovieName3
            [age] => 13.6 hrs
            [IMDBRating] => 6.2
            [IMDBLink] => http://www.imdb.com/LinkToMovie3
            [coverArt] => http://www.SomeLinkToCoverArt.com/3
        )
)

Not sure if it's because of the inner arrays causing an issue that it won't work directly with array_merge() or array_merge_recursive(). Any help would be appreciated,

Thanks.

Wallboy
  • 154
  • 5
  • 14
  • why did the merged array change its $key values from `[384][452][945]` to `[0][1][2]`? – Webeng Apr 23 '16 at 06:46
  • I'm using an ID field I assign to each key in both arrays so that when I would combine them later I could use the keys to know which IMDB/etc element combined with what later on. I'm sure I could have done it a different way, but this was the first idea in my mind to try. – Wallboy Apr 23 '16 at 07:40

4 Answers4

3

array_merge_recursive doesn't work because your outer array has numeric keys, not string keys. When array_merge or array_merge_recursive are given numeric arrays, they append them rather than merging elements with the same keys.

Instead, you can map through the arrays and merge the corresponding elements.

$result = array_map('array_merge', $array1, $array2);

Note that this code assumes that the two input arrays have all the same keys in the same order. If they're not in the same order, you can use ksort on them first to rearrange them.

If they can have different keys, though, you need a different solution, like the loop in Webang's answer.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • It might be possible that the two arrays would have different order of keys, but I suppose I could just sort them before using your solution. Thanks for the input. – Wallboy Apr 23 '16 at 07:46
3

You can try below code to merge array. Code generates desired output required to you. I have used sample array as given by you:

<?php
    $arr1=array(
        "384"=>array("name"=>"SomeMovieName1","age"=>"12.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>""),
        "452"=>array("name"=>"SomeMovieName2","age"=>"15.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>""),
        "954"=>array("name"=>"SomeMovieName3","age"=>"4.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>"")
    );
    $arr2=array(
       "384" => array("IMDBLink" => "7.2", "IMDBRating" => "http://www.imdb.com/LinkToMovie1", "coverArt" => "http://www.SomeLinkToCoverArt.com/1"),
       "452" => array("IMDBLink" => "5","IMDBRating" => "http://www.imdb.com/LinkToMovie2", "coverArt" => "http://www.SomeLinkToCoverArt.com/2"),
       "954"=>array("IMDBLink" => "8","IMDBRating" => "http://www.imdb.com/LinkToMovie3", "coverArt" => "http://www.SomeLinkToCoverArt.com/3")
    );
    $arr3 = array();
    foreach($arr1 as $key=>$val)
    {
         $arr3[] = array_merge($val, $arr2[$key]);
    }
    echo "<pre>";
    print_r($arr3);
?>
Krunal
  • 317
  • 1
  • 7
  • Thanks, that works perfect. My gut told me I was probably going to need to loop through one of the arrays with a foreach and use `array_merge()` – Wallboy Apr 23 '16 at 07:46
0

It might be easier to run your arrays in a foreach loop and just insert each value into your initial array. Lets call the first array $myFirstArray and the second array $mySecondArray:

foreach ($myFirstArray as $key => $value)
{
  $myFirstArray[$key][IMDBRating] = $mySecondArray[$key][IMDBRating];
  $myFirstArray[$key][IMDBLink] = $mySecondArray[$key][IMDBLink];
  $myFirstArray[$key][coverArt] = $mySecondArray[$key][coverArt];
}
Webeng
  • 7,050
  • 4
  • 31
  • 59
0

Didn't test this, but it should work:

Call your first array $ar1, and second $ar2

$result=array();
foreach($ar1 as $k=>$v)
{
 //if there is a corresponding array2 element
 if( isset($ar2[$k]) ){ 
       $result[] = array( $v['name], $v['age'], $ar2[$k]['IMDBLink'], $ar2[$k]['IMDBRating'],$ar2['coverArt']);
 }
}

//result
print_r($result);
Muhammed
  • 1,592
  • 1
  • 10
  • 18