这是我要在Array
中显示的HTML table
。
$itemss =array(
array( "name"=>"Blue Pen", "Description"=>"Atlas", "UnitPrice"=>13.50, " AvailableStock "=>200),
array( "name"=>"80 P-Book", "Description"=>"Atlas", "UnitPrice"=>65," AvailableStock "=>100),
array( "name"=>"A4 Paper 500", "Description"=>"Atlas", "UnitPrice"=>500," AvailableStock "=>20),
array( "name"=>"Red Pen", "Description"=>"Atlas", "UnitPrice"=>13.50," AvailableStock "=>256),
array( "name"=>"Back Pen", "Description"=>"Atlas", "UnitPrice"=>13.50," AvailableStock "=>156) );
表格中的输出应该是这样的。
Item Name |Description|Unit Price|Available Stock|Stock Value
Blue Pen Atlas 13.50 200 xxxx
Total Stock Value |xxxxxx
这就是我现在所做的事情:
<?php
$itemss =array(
array( "name"=>"Blue Pen", "Description"=>"Atlas","UnitPrice"=>13.50,"AvailableStock"=>200),
array( "name"=>"80 P-Book", "Description"=>"Atlas", "UnitPrice"=>65," AvailableStock"=>100),
array( "name"=>"A4 Paper 500","Description"=>"Atlas","UnitPrice"=>500,"AvailableStock"=>20),
array( "name"=>"Red Pen", "Description"=>"Atlas", "UnitPrice"=>13.50,"AvailableStock"=>256),
array( "name"=>"Back Pen", "Description"=>"Atlas", "UnitPrice"=>13.50,"AvailableStock"=>156) );
?>
<table border="1">
<tr>
<th>Item Name</th>
<th>Description</th>
<th>Unit Price</th>
<th>Available Stock</th>
<th>Stock Value</th>
</tr>
<?php
foreach($itemss as $Rockband)
{
echo '<tr>';
foreach(array_values($Rockband) as $key => $item)
{
echo "<td>$item</td>";
}
echo '</tr>';
}
?>
</table>
答案 0 :(得分:0)
<table border="1">
<tr>
<th>Item Name</th>
<th>Description</th>
<th>Unit Price</th>
<th>Available Stock</th>
<th>Stock Value</th>
</tr>
<?php
$TotalStockValue = 0;
foreach($itemss as $Rockband)
{
echo '<tr>';
foreach( $Rockband as $key => $value )
{
echo "<td>$value</td>";
}
$StockValue = ( $Rockband["UnitPrice"] * $Rockband["AvailableStock"] );
echo '<td>' . $StockValue . '</td>';
echo '</tr>';
$TotalStockValue += $StockValue;
}
echo '<tr><td colspan="4">Total Stock Value</td><td>' . $TotalStockValue . '</td></tr>';
?>
</table>
答案 1 :(得分:0)
您可以使用list
函数
$total_stock_value = 0;
foreach($itemss as $one_item) {
// map the array values to the variables
list($name, $desc, $price, $stock) = $one_item;
$stock_val = $price * $stock;
$total_stock_value += $stock_val;
echo '<tr><td>' . $name . '</td><td>' . $desc . '</td><td>...</td></tr>';
}
这应该打印出你所描述的表格的正文。你有一个数组数组。因此,上面的代码遍历$itemss
的每个项目,逐个读取单个数组$one_item
,并为每个数组列出(或映射,如果您愿意)该子数据的值$one_item
到给定的变量($name, $desc, ...)
。然后我只是以你在问题中描述的格式输出变量。
修改我看到了有关Stock Value
和Total Stock Value
的更新内容,因此我对我的回答进行了更精确的修改。
答案 2 :(得分:0)
你走在正确的轨道上。但是,您不需要使用array_values
。
<?php
$totalValue= 0;
foreach($itemss as $Rockband)
{
$stockValue = $Rockband["UnitPrice"] * $Rockband["AvailableStock"];
$totalValue += $stockValue;
echo '<tr>';
foreach( $Rockband as $key => $value )
{
echo "<td>$value</td>";
}
echo "<td>$stockValue</td>";
echo '</tr>';
}
echo $totalValue;
?>