duan36000 2016-04-23 06:36
浏览 45
已采纳

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

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.

  • 写回答

4条回答 默认 最新

  • douye2036 2016-04-23 07:04
    关注

    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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料