Add all instance of a variable together in a PHP foreach loop?
<p>I am trying to do something that's quite complex (for me anyway) and I've got a point which has stopped me in my tracks.</p>

<p>In this project, you enter the weight of a box, and the percentage of materials held in that box. So for example, a box may have 10% of wood, 40% of plastic, and 50% of paper. The weights of the materials are held in the database as they dont change. So the value of 1% of wood is held in the database, as 1% of plastic and paper.</p>

<p>The controller should then take the percentage of material given, and then multiply it by the 1% value held in the database.</p>

<p>So far, this is my code:</p>

<pre><code>foreach($wastestream as $ws) { 
        $genweight = $ws['Materialtype']['1pc'] * $ws['Valuegiven']['percent'];
}
</code></pre>

<p>And this gives the following values:</p>

<pre><code>10% Wood = 97.5
40% Plastic = 100.4
50% Paper = 40.3
</code></pre>

<p>The problem I now have is that I have to add all of these variables together. So essentially what I want to do after it pumps out the values is to add them all up together, so:</p>

<pre><code>97.5 + 100.4 + 40.3 = 238.2
</code></pre>

<p>What should I do?</p>

<p>Cheers!</p>
