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.

Recommended Answers

All 3 Replies

you need to add the values:

hold up ill correct that, at work, BRB
commented: Thanks +0
<div>value  |  number</div>
<?php
$arr = array(10, 7, 2, 4);
$i = 0;
foreach ($arr as $key => $value) {
    $i += $value;
    echo "<p>" . $key . "  |  " . $i;
}
?>
commented: Great! +1

You're the best. Thanks very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.