使用相同的数字键合并两个PHP数组 [英] Merging two PHP arrays with same numeric key
问题描述
尝试用相同的数字键合并两个数组有一些困难.我已经尝试过array_merge()
和array_merge_recursive()
,但是似乎要做的就是附加第二个数组.
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
)
)
不确定是否是由于内部数组引起的问题,导致它不能直接与array_merge()
或array_merge_recursive()
一起使用.任何帮助将不胜感激,
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,
谢谢.
推荐答案
您可以尝试使用以下代码合并数组.代码生成所需的输出给您.我使用了您提供的示例数组:
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数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!