3

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
3
  • 65, 95 -- where do they come from? Commented Mar 6, 2015 at 9:56
  • $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? Commented Mar 6, 2015 at 9:56
  • When I run the above code, it gives me the output in that way.. They get added to previous value.. Commented Mar 6, 2015 at 9:57

2 Answers 2

2

Just move the echo'ing part out of your foreach loop :

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>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir.. Your Solution helped me.. Can you please help me out with this solution also ? Check the UPDATE 2 of the question.. Here's the link
0

How about this:

Let's say this is your array:

$ar = array(
    array ( 
        15 => 'Due', 
        50 => 'Due' ),
    array ( 
        20 => 'Cancelled', 
        30 => 'Due' )
);

I modified your markup to this:

<table border="1" cellpadding="5">
    <thead>
        <tr>
            <th>Orders</th>
            <th>DueAmount</th>
        </tr>
    </thead>
    <tbody>
    <?php
        // This variable will handle the due's
        $due = 0;

        foreach ($ar as $row_number => $data) {
            // I used the main key of your array as row counter, or if you don't trust this
            // you can just declare a counter outside the foreach 
            // and increment here. This will solve your first bug.
            $row_number++;
            foreach ($data as $amount => $header) {
                // Same as your logic, accumulate the due in a variable.
                // This will solve your second problem. 
                if ($header == 'Due') {
                    $due += $amount;   
                }
            }
            echo '<tr>';
                echo "<td>{$row_number}</td>";
                echo "<td>{$due}</td>";
            echo '<tr>';
        }
    ?>
    </tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.