PHP Merge (Add) Two Arrays by Same Key -
i have 2 arrays.
$a = array('a' => 2, 'b' => 5, 'c' => 8); $b = array('a' => 3, 'b' => 7, 'c' => 10); i want merge these 2 arrays , following result.
$c = array('a' => 5, 'b' => 12, 'c' => 18); what easiest way archive this?
thanks!
as mentioned in comments, looping through array trick.
$a = array('a' => 2, 'b' => 5, 'c' => 8); $b = array('a' => 3, 'b' => 7, 'c' => 10); $c = array(); foreach($a $index => $item) { if(isset($b[$index])) { $new_value = $a[$index] + $b[$index]; $c[$index] = $new_value; } }
Comments
Post a Comment