合并两个具有相同数字键的 PHP 数组

2022-01-07 00:00:00 merge arrays nested php

尝试合并具有相同数字键的两个数组时遇到一些困难.我尝试过 array_merge()array_merge_recursive(),但似乎所做的只是附加第二个数组.

第一个数组的形式如下:

数组([384] =>大批([名称] =>电影名称 1[年龄] =>12.2 小时[IMDBLink] =>[IMDBRating] =>[封面艺术] =>)[452] =>大批([名称] =>SomeMovieName2[年龄] =>13.1 小时[IMDBLink] =>[IMDBRating] =>[封面艺术] =>)[945] =>大批([名称] =>SomeMovieName3[年龄] =>13.6 小时[IMDBLink] =>[IMDBRating] =>[封面艺术] =>))

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

数组([384] =>大批([IMDBRating] =>7.2[IMDBLink] =>http://www.imdb.com/LinkToMovie1[封面艺术] =>http://www.SomeLinkToCoverArt.com/1)[452] =>大批([IMDBRating] =>8.2[IMDBLink] =>http://www.imdb.com/LinkToMovie2[封面艺术] =>http://www.SomeLinkToCoverArt.com/2)[945] =>大批([IMDBRating] =>6.2[IMDBLink] =>http://www.imdb.com/LinkToMovie3[封面艺术] =>http://www.SomeLinkToCoverArt.com/3))

合并后,我希望结果是:

数组([0] =>大批([名称] =>电影名称 1[年龄] =>12.2 小时[IMDBRating] =>7.2[IMDBLink] =>http://www.imdb.com/LinkToMovie1[封面艺术] =>http://www.SomeLinkToCoverArt.com/1)[1] =>大批([名称] =>SomeMovieName2[年龄] =>13.1 小时[IMDBRating] =>8.2[IMDBLink] =>http://www.imdb.com/LinkToMovie2[封面艺术] =>http://www.SomeLinkToCoverArt.com/2)[2] =>大批([名称] =>SomeMovieName3[年龄] =>13.6 小时[IMDBRating] =>6.2[IMDBLink] =>http://www.imdb.com/LinkToMovie3[封面艺术] =>http://www.SomeLinkToCoverArt.com/3))

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

谢谢.

解决方案

您可以尝试以下代码合并数组.代码生成您所需的所需输出.我使用了您提供的示例数组:

""),"452"=>array("name"=>"SomeMovieName2","age"=>"15.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>""),"954"=>array("name"=>"SomeMovieName3","age"=>"4.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>""));$arr2=数组(384" =>数组(IMDBLink"=>7.2",IMDBRating"=>http://www.imdb.com/LinkToMovie1",coverArt"=>http://www.SomeLinkToCoverArt.com/1"),452" =>数组(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 = 数组();foreach($arr1 as $key=>$val){$arr3[] = array_merge($val, $arr2[$key]);}echo "

";打印_r($arr3);?>

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.

解决方案

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

相关文章