我有这个数组:
Array (amounts)
(
[0] => Array
(
[0] => 95
[1] => 2
)
[1] => Array
(
[0] => 96
[1] => 5
)
)
而这个
Array (invoices)
(
[1] =>
[2] => 490
[3] =>
[4] =>
[5] => 1400
)
这就是我想要得到的:
Array
(
[1] =>
[95] => 490 // Id found in Amounts array so replaced id by Key '0'
[3] =>
[4] =>
[96] => 1400 // Id found in Amounts array so replaced id by Key '0'
)
我试图处理在这里发现的answser,但没有成功。
$newamounts = array_combine(array_map(function($key) use ($invoices) {
return $invoices[$key]; // translate key to name
}, array_keys($invoices)), $amounts);
任何帮助非常感谢。感谢
这应该适合您:
(在这里,我使用 foreach 循环遍历每个 innerArray 的$amounts
数组,然后检查 innerArray 索引为 1 的数组元素是否$invoices
不为空,如果不是,我使用键和值设置新元素并取消设置旧元素)
<?php
$amounts = array(
array(
95,
2
),
array(
96,
5
)
);
$invoices = array(1 =>"", 2 => 490, 3 => "", 4 => "", 5 => 1500);
foreach($amounts as $innerArray) {
if(!empty($invoices[$innerArray[1]])) {
$invoices[$innerArray[0]] = $invoices[$innerArray[1]];
unset($invoices[$innerArray[1]]);
}
}
print_r($invoices);
?>
输出:
Array ( [1] => [3] => [4] => [95] => 490 [96] => 1500 )