Take the 2-minute tour ×

I am looking to add up all the $profit with its associated array ($mfgname).

while ($i < $num) {
    $array[$mfgname] = $profittotal + $profit;
    echo $mfgname . " | " . $profit . "<br/>";
$i++; }

Spits out:

Mfgname1 | 5.00
Mfgname3 | 1.00
Mfgname1 | -1.00
Mfgname2 | 10.00
Mfgname3 | 4.50
Mfgname2 | -5.00

Profits are correct, however the array is only spitting out the last profit shown for the Mfg:

Array (
        [Mfgname1] => -1.00
        [Mfgname2] => -5.00
        [Mfgname3] => 4.50

I am trying to add them up if it is associated with that Mfgname. I believe I have to find the array value in the while loop and try and add it to the newest output $profit. Any ideas?

share|improve this question
Where is $profittotal defined? –  andrewsi Sep 13 '12 at 18:01
add comment

2 Answers

up vote 2 down vote accepted

I am not sure what are you trying to do but here goes my guess

$array[$mfgname] += $profit;

Instead of

share|improve this answer
add comment

If I understand what it is you want, I think you are using the wrong variables in the wrong place. Try this:

$profittotal = $array[$mfgname] + $profit;
echo $mfgname . " | " . $profittotal . "<br/>";
share|improve this answer
add comment

Your Answer


