0

What is the easiest way to achieve:

a => 1, b =>  0, c=> 3
a => 0, b => 10, c=> 1

Sum

a => 1, b =>10, c=>4

and

Minus

a => -1, b=> 10, c=> -2

I hope my examples make it clear... If you have any questions please leave a comment

1
  • for the minus part, i think u refer to (a*-1)-(a*-1)... ? Commented Nov 9, 2010 at 19:15

3 Answers 3

2

Sum:

$array1 = array('a' => 1, 'b' => 0, 'c' => 3);
$array2 = array('a' => 0, 'b' => 10, 'c' => 1);

$result = array();
foreach ($array1 as $key => $value)
  $result[$key] = $value + $array2[$key];

You can implement the difference part similarly.

Sign up to request clarification or add additional context in comments.

Comments

1
 $sum = $minus = 0;
 foreach ($arrays as $key=>$val)
 {
   $sum   += $val;
   $minus -= ($val*-1);
 }

Comments

1

You want to add orsubstarct the values with same key.

Try to write function with array_walk

http://php.net/manual/en/function.array-walk.php

or put in a loop and add or substarct based on key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.