如何在数组上执行计算并以表格形式显示它

时间:2017-03-01 13:47:45

标签: php html5

我有关于数组乘法的分配并以表格形式显示,我的老师只是给我数组形式的数据并表示以表格形式显示它并且还乘以项目**数量和价格* *并在表格中显示....我已经以表格形式显示数组,但我无法提供数量和价格?有人帮忙吗?

<!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
$ProductsTest = array("1001" => array("Name" => "LCD","Desc" => "Sony","Qty" =>10,"Price" => 2000),
"1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300),
"1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300),
"1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700),
"1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000));
$no_of_ele = count($ProductsTest);
echo '<br><br>';
echo ' <table border="2", bgcolor="white",width="100%"> 
<tr>
 <th>Name</th> 
    <th>Brand</th>
     <th>QTY</th> 
    <th>price</th>
    <th>totalamount</th>
  </tr>';
foreach( $ProductsTest as $values)
{
    echo '<tr>';
    foreach( $values as $key )
    {
        echo '<td>'.$key.'</td>';
  }
    echo '</tr>';
}

echo '</table>';
?>

</body>
</html>

这是我的输出到现在

3 个答案:

答案 0 :(得分:1)

<?php
$ProductsTest = array("1001" => array("Name" => "LCD","Desc" => "Sony","Qty" =>10,"Price" => 2000),
"1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300),
"1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300),
"1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700),
"1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000));
$no_of_ele = count($ProductsTest); 
?>
<br><br>
<table border="2", bgcolor="white",width="100%"> 
<tr>
 <th>Name</th> 
    <th>Brand</th>
     <th>QTY</th> 
    <th>price</th>
    <th>totalamount</th>
  </tr>
<?php
foreach( $ProductsTest as $key => $value)
{
    echo '<tr>';
    echo '<td>'.$value['Name'].'</td>';
    echo '<td>'.$value['Desc'].'</td>';
    echo '<td>'.$value['Qty'].'</td>';
    echo '<td>'.$value['Price'].'</td>';
    echo '<td>'.$value['Qty']*$value['Price'].'</td>';
    echo '</tr>';
}
?>
</table>

答案 1 :(得分:0)

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    $ProductsTest = array(
    "1001" => array("Name" => "LCD", "Desc" => "Sony", "Qty" =>10, "Price" => 2000),
    "1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300),
    "1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300),
    "1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700),
    "1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000)
    );

    $no_of_ele = count($ProductsTest);

    echo '<br><br>';

    if( $no_of_ele > 0 )
    {
    echo '<table border="2", bgcolor="white",width="100%"> 
    <tr>
    <th>Name</th> 
    <th>Brand</th>
    <th>QTY</th> 
    <th>price</th>
    <th>totalamount</th>
    </tr>';

    foreach( $ProductsTest as $key_ProductsTest => $value_ProductsTest)
    {
    echo '<tr>';
    echo '<td>'.$value_ProductsTest["Name"].'</td>';
    echo '<td>'.$value_ProductsTest["Desc"].'</td>';
    echo '<td>'.$value_ProductsTest["Qty"].'</td>';
    echo '<td>'.$value_ProductsTest["Price"].'</td>';
    echo '<td>'. ($value_ProductsTest["Qty"] * $value_ProductsTest["Price"]) .'</td>';
    echo '</tr>';
    }

    echo '</table>';
    }
    else
    {
    echo "Sorry, no data found.";
    }


    ?>

    </body>
    </html>

答案 2 :(得分:0)

对于使用函数式编程的这个问题,这是一个很好的解决方案,您可以映射数组以使用totalamount返回其他数组

    <!doctype html>
        <html>
        <head>
        <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    $lists = array("1001" => array("Name" => "LCD","Desc" => "Sony","Qty" =>10,"Price" => 2000),
    "1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300),
    "1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300),
    "1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700),
    "1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000));

    $ProductsTest  = array_map(function($item){
        $item['totalamount'] = $item['Qty'] * $item['Price'];
        return $item;
    },$lists);
$no_of_ele = count($ProductsTest);
    echo '<br><br>';
    echo ' <table border="2", bgcolor="white",width="100%"> 
    <tr>
     <th>Name</th> 
        <th>Brand</th>
         <th>QTY</th> 
        <th>price</th>
        <th>totalamount</th>
      </tr>';
    foreach( $ProductsTest as $values)
    {
        echo '<tr>';
        foreach( $values as $key )
        {
            echo '<td>'.$key.'</td>';
        }
        echo '</tr>';
    }

    echo '</table>';
    ?>

    </body>
    </html>