使用相同的数字键合并两个PHP数组

时间:2016-04-23 06:36:06

标签: php arrays merge nested

尝试使用相同的数字键合并两个数组时遇到一些困难。我尝试了array_merge()array_merge_recursive(),但似乎所做的只是追加第二个数组。

第一个数组具有以下形式:

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] => 
        )
)

这是我要与第一个组合/合并的第二个数组:

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

合并后,我希望结果如下:

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

不确定是否因为内部数组导致问题而无法直接使用array_merge()array_merge_recursive()。任何帮助将不胜感激,

感谢。

4 个答案:

答案 0 :(得分:2)

您可以尝试下面的代码来合并数组。代码生成所需的所需输出。我使用了你给出的样本数组:

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

答案 1 :(得分:0)

在foreach循环中运行数组可能更容易,只需将每个值插入初始数组即可。让我们调用第一个数组$mySecondArray和第二个数组foreach ($myFirstArray as $key => $value) { $myFirstArray[$key][IMDBRating] = $mySecondArray[$key][IMDBRating]; $myFirstArray[$key][IMDBLink] = $mySecondArray[$key][IMDBLink]; $myFirstArray[$key][coverArt] = $mySecondArray[$key][coverArt]; }

Activity

答案 2 :(得分:0)

没有对此进行测试,但它应该有效:

调用你的第一个数组$ ar1,然后调用第二个$ ar2

{{1}}

答案 3 :(得分:0)

array_merge_recursive不起作用,因为外部数组有数字键,而不是字符串键。当array_mergearray_merge_recursive被赋予数字数组时,它们会附加它们而不是使用相同的键合并元素。

相反,您可以映射数组并合并相应的元素。

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

请注意,此代码假定两个输入数组具有相同顺序的所有相同键。如果它们的顺序不同,您可以先使用ksort重新排列它们。

如果他们可以有不同的键,你需要一个不同的解决方案,比如Webang的回答中的循环。