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

Merging two PHP arrays with same numeric key(合并两个具有相同数字键的 PHP 数组)
本文介绍了合并两个具有相同数字键的 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);
?>

这篇关于合并两个具有相同数字键的 PHP 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)