【发布时间】:2020-12-01 16:03:46
【问题描述】:
我正在尝试合并这两个数组:
$array1 = array (
123 => array (
'minutes_watched' => 192.0,
'impressions_count' => 18
),
456 => array (
'minutes_watched' => 200.0,
'impressions_count' => 20
)
);
$array2 = array (
123 => array (
'ingested_trailers_count' => 3,
'ingested_shorts_count' => 2,
'ingested_features_count' => 1
),
456 => array (
'ingested_trailers_count' => 10,
'ingested_shorts_count' => 10,
'ingested_features_count' => 10
)
);
我想这样结束:
$merged = array (
123 => array (
'minutes_watched' => 192.0,
'impressions_count' => 18,
'ingested_trailers_count' => 3,
'ingested_shorts_count' => 2,
'ingested_features_count' => 1
),
456 => array (
'minutes_watched' => 200.0,
'impressions_count' => 20,
'ingested_trailers_count' => 10,
'ingested_shorts_count' => 10,
'ingested_features_count' => 10
)
);
最终有了这个:
$merged = array (
array(
'id' => 123
'minutes_watched' => 192.0,
'impressions_count' => 18,
'ingested_trailers_count' => 3,
'ingested_shorts_count' => 2,
'ingested_features_count' => 1
),
array(
'id' => 456
'minutes_watched' => 200.0,
'impressions_count' => 20,
'ingested_trailers_count' => 10,
'ingested_shorts_count' => 10,
'ingested_features_count' => 10
)
);
如果没有一些笨拙的 for 循环,我似乎找不到一种简洁的方法。肯定有一些 PHP 数组函数可以处理这个问题吗?
【问题讨论】:
-
你需要一个笨重的循环。
-
567会发生什么? -
道歉 - 似乎我的钥匙都搞砸了 - 再次更正。我想对他们分组。
-
这能回答你的问题吗? PHP : multidimensional array merge recursive