Saiyan Prince
Saiyan Prince

Reputation: 4020

Add values in second array depending upon the values in first array

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)
);

Upvotes: 1

Views: 81

Answers (3)

George Cummins
George Cummins

Reputation: 28906

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);

Upvotes: 1

billyonecan
billyonecan

Reputation: 20250

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

Upvotes: 0

Joel Harkes
Joel Harkes

Reputation: 11661

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

Upvotes: 0

Related Questions