php - is it possible to array push value into existing array value with sum? -
is possible array push value existing array value sum?
$nvarr = array("1"=> 35, "2"=> 1, "3"=> 2, "8"=> 56, "9"=> 78);
i have add 1 more array value existing array
$nvarr1 = array("1"=> 35, "2"=> 1);
and want following out put
$nvarr = array("1"=> 70, "2"=> 2, "3"=> 2, "8"=> 56, "9"=> 78);
you can foreach
foreach($nvarr1 $key => $val){ (array_key_exists($key, $invarr))? $invarr[$key] += $nvarr1[$key] : $invarr[$key] = $nvarr1[$key] }
Comments
Post a Comment