I’m using a foreach loop to write out the rows of a table which contains some numeric data from arrays amongst other things. For a couple of columns I need to sum the value of the numeric data in the variables, which ordinarily wouldn’t be a problem using a for loop, but because I’m using a foreach loop in the table, of course all the variables I’m wanting to add have the same name, $quantity. There’s probably a ridiculously easy answer to this that I’m missing and can’t see the wood for the trees.
Use an array, $quantity[0] = first variable, $quantity[1] = second.
Try using this in your foreach loop.
$x = 0; (before the foreach loop)
$quantity[$x] = value
$x++;
I don’t get any errors with that, but neither do I get a total - I’m presuming it’s possibly a problem with local variables as the $a array’s outside the foreach loop while the $quantity is inside the loop, as when I try one of the examples given in the PHP manual:
The problem with the first block is that you used array_sum in the string.
Which just outputs array_sum.
In the second block, array_sum is out of the string and therefore parsed instead of just written on the screen.