0

I am trying to count the total sum of values using array_combine. I have the duplicate key also, for example i have the array of emailid and array of product price.

$data1 = array("[email protected]","[email protected]","[email protected]");

and $data2 = array("100","200","300");

Now in $data1 i have two duplicate values as [email protected]

I am trying to use array_combine() It ignore the duplicate values and add the new one

I get this result as

[email protected] => 300;
[email protected] => 200;

But i want the result should be

[email protected] => (400)100+300;
[email protected] => 200;

Not sure how to get this result with array_combine Is something any alternatives to achieve this ?

Any Suggestion would be great.

1
  • 3
    array_combine simply won't help you here. It's trivial if you write a manual loop though. Commented Aug 25, 2014 at 9:21

1 Answer 1

4

Write a simple loop:

$result = array();

foreach ($data1 as $i => $v) {
  if (!isset($result[$v])) {
     $result[$v] = 0;
  }
  $result[$v] += $data2[$i];
}
Sign up to request clarification or add additional context in comments.

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.