I have an array like this:
Array
(
[0] => Array
(
[15] => Due
[50] => Due
)
[1] => Array
(
[20] => Cancelled
[30] => Due
)
)
I want to display addition of the due amount on the parent array basis in the tabular format, like this:
Orders DueAmount
0 65
1 95
The code that I have tried:
<table border="1" cellpadding="5">
<thead>
<tr>
<th>Orders</th>
<th>DueAmount</th>
</tr>
</thead>
<tbody>
<?php
$tot = 0;
for ($i=0; $i < count($ar); $i++) { // $ar is the parent array
foreach ($ar[$i] as $key => $value) {
if ($value === "Due") {
$amt = $tot += $key;
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$amt."</td>";
echo "</tr>";
}
}
}
?>
</tbody>
</table>
The above code when executed, gives the output as:
Orders DueAmount
0 15
0 65
1 95
How do I solve this ? Kindly help me out.
UPDATE 1:
After vascowhile's comment: I get the following output
Orders Due Amount
0 15
0 50
1 30
$tot = 0;should be inside your foreach loop. I should add that I don't think it is a good idea to use array keys as values that way. What happens in the future if you need to change to decimal values?