0

HEllo.

If I do this..

<div>value  |  number</div>

<?php
$arr = array(10, 7, 2, 4);
$i = 0;
foreach ($arr as $value) {
    $i++;
    echo "<p>" . $value . "  |  " . $i;
}
?>

ill get something like

number | value
1      | 10

2      | 7

3      | 2

4      | 4

How would I make it so the values are added up along the way.. end result being

number | value
1      | 10 //(old value 10)

2      | 17 //(7)  

3      | 19 //(2)

4      | 23 //(4)





$value++?

Thanks.

Edited by reco21

2
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by reco21
1

you need to add the values:

hold up ill correct that, at work, BRB

Edited by DarkMonarch: mistake

Votes + Comments
Thanks
1
<div>value  |  number</div>
<?php
$arr = array(10, 7, 2, 4);
$i = 0;
foreach ($arr as $key => $value) {
    $i += $value;
    echo "<p>" . $key . "  |  " . $i;
}
?>

Edited by DarkMonarch: type too fast

Votes + Comments
Great!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.