Merging two PHP arrays with same numeric key(合并两个具有相同数字键的 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 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:合并两个具有相同数字键的 PHP 数组
基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01