Home > database >  How do you multiply quantity and price within the same array using php?
How do you multiply quantity and price within the same array using php?

Time:04-18

Here is my multidimensional array

<?php
    $total = array (

                      array(
                        "prod_price" => 15,
                        "quantity" => 3
                      ),

                      array(
                        "prod_price" => 8,
                        "quantity" => 2
                      )
?>

I'm trying to display all of these in the form of a table

<table>
     <tr>
       <th>Price</th>
       <th>Quantity</th>
       <th>Sub Total</th>
     </tr>
     <?php
     foreach($total as $p){
       ?>
     <tr>
       <td><?php echo $p["prod_price"];?></td>
       <td><?php echo $p["quantity"]; ?></td>

     </tr>
    <?php
   }
   ?>
   </table>

But I have no idea how to multiply quantity and price and display it according to their table like this.... |Price|Quantity|Sub Total| |:----|:-------|:--------| |15 | 3 | | |8 | 2 | |

Table

I would love if anyone could give me suggestions to how I could do that by still using the multidimensional array.

CodePudding user response:

You can simply do this

<table>
  <tr>
    <td>Price</td>
    <td>Quantity</td>
    <td>Total</td>
   </tr>
   <?php foreach($total as $data){?>
   <tr>
    <td><?php echo $data['prod_price'];?></td>
    <td><?php echo $data['quantity'];?></td>
    <td><?php echo $data['prod_price'] * $data['quantity'];?></td>
   </tr>    
   <?php }?>
</table>

  • Related