Re: Sum two arrays

From: Date: Sat, 12 Apr 2003 08:42:55 +0000
Subject: Re: Sum two arrays
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-143604@lists.php.net to get a copy of this message
I'm sure there is a cleaner, better way ... but here's how I did it in 1 minute. $a = array("01"=>10,"02"=>20,"04"=>40); $b = array("01"=>10,"03"=>30,"05"=>50); $c = array(); foreach ($a as $idx => $val) {
        if (isset ($c[$idx])) { $c[$idx] = $c[$idx] + $a[$idx]; }
        else { $c[$idx] = $a[$idx]; }
} foreach ($b as $idx => $val) {
        if (isset ($c[$idx])) { $c[$idx] = $c[$idx] + $b[$idx]; }
        else { $c[$idx] = $b[$idx]; }
} HTH, Jason k Larson Larry_Li@amat.com wrote:
$a=array("01"=>10,"02"=>20,"04"=>40); $b=array("01"=>10,"03"=>30,"05"=>50); $c=$a+$b; $c=array("01"=>20,"02"=>20,"03"=>30,"04"=>40,"05"=>50); Thanks, Larry


« previous php.general (#143604) next »