我有 2 个数组如下(数组 1,数组 2),我想根据以下条件合并为一个数组如下(新数组)条件:(如果 Array-1(hID) = Array-2(hID) 然后 megre 如下)阵列 1Array( [0] => Array ( [hID] => 107 [env] => DEV [serv] => server1 ) [1] => Array ( [hID] => 122 [env] => DEV [serv] => server2 )阵列 2Array( [107] => Array ( [hID] => 107 [cpu] => 32 [mem] => 24 ) [122] => Array ( [hID] => 122 [cpu] => 16 [mem] => 24 )新阵列Array( [0] => Array ( [hID] => 107 [env] => DEV [serv] => server1 [cpu] => 32 [mem] => 24 ) [1] => Array ( [hID] => 122 [env] => DEV [serv] => server2 [cpu] => 16 [mem] => 24 )
3 回答
皈依舞
TA贡献1851条经验 获得超3个赞
如果第二个数组始终具有与hID值相同的键,那么您可以执行以下操作:
$newArray = [];
foreach ($array1 as $item) {
if (isset($array2[$item['hID']])) {
$newArray[] = array_merge($item, $array2[$item['hID']]);
}
}
或者你总是可以创建这样的数组(键与 hID 值相同):
$array2 = array_combine(
array_column($array2, 'hID'),
$array2
);
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
if($array1[hID] == $array2[hID]){
$Array3 = array_merge($array1, $array2); }
- 3 回答
- 0 关注
- 265 浏览
添加回答
举报
0/150
提交
取消