이것은 HTML table
에 표시하려는 Array
입니다.다차원 배열 계산
$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
그리고이 I가 한 일을 지금에 : 당신은 아마 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>';
}
이를 사용하여,이 같은 배열에 foreach 문을 사용할 수
<?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>
무엇이 질문입니까? – billyonecan
배열에 'Stock Value'가 없으므로 하위 배열에는 4 개의 요소 만 포함됩니다. – jeroen
재고 값 = 단가 * 재고 있음 – user3312802