1

I have an array like this. I'm trying to find sum of all elements having same array key.

Expected Output: Array ( [JOHN] => 25000 [SMITH] => 13000 [ALEX] => 40000 [JAMES] => 16000 [ANTONY] => 28000 )

But what I'm getting is something different.

$amounts = array( 'JANUARY' => array('JOHN' => array(2000, 5000, 3000), 'SMITH' => array(2000, 1000, 1000, 6000)), 'FEBRUARY' => array('ALEX' => array(5000, 7000, 4000), 'JAMES' => array(4000, 1000, 3000, 2000)), 'MARCH' => array('ANTONY' => array(7000, 2000, 4000), 'JOHN' => array(3000, 5000, 6000, 1000)), 'APRIL' => array('ANTONY' => array(2000, 4000, 6000, 3000), 'ALEX' => array(7000, 8000, 9000)), 'MAY' => array('SMITH' => array(1000, 2000), 'JAMES' => array(2000, 3000, 1000)) );


foreach($amounts as $key => $value)
{
    $sum=array();
    foreach($value as $key1 => $value1)
    {
        $sum[$key1] = array_sum($value1);

    }
    print_r($sum);
}

Output I'm getting is:

Array
(
    [JOHN] => 10000
    [SMITH] => 10000
)
Array
(
    [ALEX] => 16000
    [JAMES] => 10000
)
Array
(
    [ANTONY] => 13000
    [JOHN] => 15000
)
Array
(
    [ANTONY] => 15000
    [ALEX] => 24000
)
Array
(
    [SMITH] => 3000
    [JAMES] => 6000
)

Expected Output: Array ( [JOHN] => 25000 [SMITH] => 13000 [ALEX] => 40000 [JAMES] => 16000 [ANTONY] => 28000 )

2
  • Whats is your expected output?? Commented Feb 10, 2017 at 6:07
  • Array ( [JOHN] => 25000 [SMITH] => 13000 [ALEX] => 40000 [JAMES] => 16000 [ANTONY] => 28000 ) @M A SIDDIQUI Commented Feb 10, 2017 at 6:08

2 Answers 2

2

Is this what you want?

$amounts = array( 'JANUARY' => array('JOHN' => array(2000, 5000, 3000), 'SMITH' => array(2000, 1000, 1000, 6000)), 'FEBRUARY' => array('ALEX' => array(5000, 7000, 4000), 'JAMES' => array(4000, 1000, 3000, 2000)), 'MARCH' => array('ANTONY' => array(7000, 2000, 4000), 'JOHN' => array(3000, 5000, 6000, 1000)), 'APRIL' => array('ANTONY' => array(2000, 4000, 6000, 3000), 'ALEX' => array(7000, 8000, 9000)), 'MAY' => array('SMITH' => array(1000, 2000), 'JAMES' => array(2000, 3000, 1000)) );

$sum=array();
foreach($amounts as $key => $value)
{    
    foreach($value as $key1 => $value1)
    {
      //check key set or not. if not set then assign value 0. so that you dont get undefine index error
        if(!isset($sum[$key1]))
          $sum[$key1] = 0;
        $sum[$key1] += array_sum($value1);

    }    

}
print_r($sum);
Sign up to request clarification or add additional context in comments.

Comments

1

Your attempt is close, just need this modification:

//initialise this  before foreach, so that it doesn't gets reset in between.
$sum=array(); 
foreach($amounts as $key => $value) {
  foreach($value as $key1 => $value1) {
    //Add the previous sum too, so that it doesn't calculate the new sum
    if(!array_key_exists($key1, $sum)) {
      $sum[$key1] = 0;
    }
    $sum[$key1] += array_sum($value1);
  }
}
//Debug this at the last and not inside the loop.
print_r($sum);

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.