1

Here's the situation:

Suppose I have first Array as:

Array(
    [0] => Person1
    [1] => Person1
    [2] => Person2
)

And second array as:

Array(
    [0] => 100.00
    [1] => 150.25
    [2] => 157.15
)

How do I add values (100.00 + 150.25) of second Array and merge them (250.25) so that they belong to Person1 in the first array.

Desired Output:

Array(
    [0] => 250.25 // for Person1 in the first Array after adding
    [1] => 157.15 // for Person2 in the first Array
)

Any help is highly appreciated. Thank You.

P.S.: All the values are coming from the database.

EDIT 1: Here's what I have tried, but this outputs the second array as it is:

$sums = array();
$sums = array_fill_keys(array_keys($affiCode + $affiCommAmount), 0);
array_walk($sums, function (&$value, $key, $arrs) {
    $value = @($arrs[0][$key] + $arrs[1][$key]);
    }, array($affiCode, $affiCommAmount)
);
4
  • paste the code you have.Use a loop and add the first and second value; and put it in the first index Commented Jan 19, 2015 at 10:54
  • Can you tell us what you already tried? Or are you asking a question before you even tried something :) ? Commented Jan 19, 2015 at 10:55
  • Please check the question now.. Commented Jan 19, 2015 at 10:57
  • The suppression operator (@) is used to hide errors, warnings, and notices. It can make problems hard to spot when debugging code. Consider removing it, at least while debugging. Commented Jan 19, 2015 at 11:11

3 Answers 3

1

The arrays are the same size, so you can use a for loop to process them simultaneously:

for($i = 0; $i<count($personArray); $i++)

Within the loop, construct a new array keyed to the values from the first array. If the key does not yet exist, initialize it:

if (!isset($newArray[$personArray[$i]])) {
    $newArray[$personArray[$i]] = 0.0;
}

then add the new value to the selected array key:

$newArray[$personArray[$i]] += $valueArray[$i]

When the loop ends, $newArray will look like:

Array(
    ['Person1'] => 250.25
    ['Person2'] => 157.15
)

If you want to replace the 'Person1' and 'Person2' keys with numerical indexes, use array_values():

$numericallyIndexedArray = array_values($newArray);

The final code looks like:

$newArray = [];
for($i = 0; $i<count($personArray); $i++) {
    if (!isset($newArray[$personArray[$i]])) {
        $newArray[$personArray[$i]] = 0;
    }

    $newArray[$personArray[$i]] += $valueArray[$i];
}

// Optionally return the new array with numerical indexes:
$numericallyIndexedArray = array_values($newArray);
Sign up to request clarification or add additional context in comments.

Comments

0

Get the person from first array by index and add the money:

for($i=0;$i<count(arrayPerson);$i++){
  $arrayPerson[$i]->addMoney($arrayMoney[$i])
  //Or  $arrayPerson[$i]->Money += $arrayMoney[$i]
} //$i defines your index in the array.

sql

Better though to make a join in the SQL and sum the money and group by PersonID.

for example:

SELECT person.* COUNT(Money) FROM Person
LEFT JOIN Money 
ON person.ID = Money.PersonID
GROUP BY person.ID

Comments

0

Simple foreach loop:

$people = array('Person1', 'Person1', 'Person2');
$values = array(100.00, 150.25, 157.15);

$output = array();
foreach ($people as $key => $person) {
    
    if (! isset($output[$person])) {
        $output[$person] = 0;
    }
    
    if (! empty($values[$key])) {
        $output[$person] += $values[$key];
    }
}

// $output = array(2) { ["Person1"]=> float(250.25) ["Person2"]=> float(157.15) }

If you want to do away with the keys in $output, you can use array_values($output)

Here's an example

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.