我有几个月的数组=>价值观,如此......
$months = array(
'January' => 0.00,
'February' => 0.00,
'March' => 0.00,
'April' => 0.00,
'May' => 0.00,
'June' => 0.00,
'July' => 0.00,
'August' => 0.00,
'September' => 0.00,
'October' => 0.00,
'November' => 0.00,
'December' => 0.00,
)
我想与临时数组中的值结合...
$temp = array(
'February' => 200.00,
'May' => 17.50,
)
这样我就得以下......
$months = array(
'January' => 0.00,
'February' => 200.00, // Combined
'March' => 0.00,
'April' => 0.00,
'May' => 17.50, // Combined
'June' => 0.00,
'July' => 0.00,
'August' => 0.00,
'September' => 0.00,
'October' => 0.00,
'November' => 0.00,
'December' => 0.00,
)
答案 0 :(得分:1)
一个简单的方法是foreach:
foreach ($months as $month => $value) {
// I check if the $temp array has the $month as key too
if (key_exists($month, $temp)) {
// I combine the $temp array value with the $months array value for the $month key
$months[$month] += $temp[$month];
}
}
循环后输出var_dump($months)
:
array (size=12)
'January' => float 0
'February' => float 200
'March' => float 0
'April' => float 0
'May' => float 17.5
'June' => float 0
'July' => float 0
'August' => float 0
'September' => float 0
'October' => float 0
'November' => float 0
'December' => float 0
答案 1 :(得分:1)
$result = array_replace($months, $temp);
print_r($result);
...给出
Array
(
[January] => 0
[February] => 200
[March] => 0
[April] => 0
[May] => 17.5
[June] => 0
[July] => 0
[August] => 0
[September] => 0
[October] => 0
[November] => 0
[December] => 0
)
答案 2 :(得分:1)
您可以使用array_merge
结果$months = array(
'January' => 0.00,
'February' => 0.00,
'March' => 0.00,
'April' => 0.00,
'May' => 0.00,
'June' => 0.00,
'July' => 0.00,
'August' => 0.00,
'September' => 0.00,
'October' => 0.00,
'November' => 0.00,
'December' => 0.00,
);
$temp = array(
'February' => 200.00,
'May' => 17.50,
);
$result = array_merge($months, $temp);
echo "<pre>";
print_r($result);
echo "</pre>";
exit;