我有这个数组:
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'
)
我试图处理找到here的答案,但没有成功。
$newamounts = array_combine(array_map(function($key) use ($invoices) {
return $invoices[$key]; // translate key to name
}, array_keys($invoices)), $amounts);
任何帮助非常感谢。 THX
答案 0 :(得分:1)
这应该适合你:
(这里我使用foreach循环遍历$amounts
的每个innerArray然后我检查$invoices
中的innerArray的索引1的数组元素是否为空,如果不是,我设置了具有键和值的新元素并取消旧元素的设置)
<?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 )