我的代码如下:
<?php
$items = array(
array("SKU" => "a49w8dsa", "Title" => "Socks", "Description" => "Sports socks", "Price" => "1.50", "Quantity" => "4"),
array("SKU" => "ta8dla", "Title" => "T-shirt", "Description" => "ABC Brand undershirt", "Price" => "14.25", "Quantity" => "2"),
array("SKU" => "yusa982", "Title" => "Flip Flips", "Description" => "XYZ Brand Beach Flops", "Price" => "2.88", "Quantity" => "5"),
array("SKU" => "gnbaiue", "Title" => "Ball Cap", "Description" => "No Name", "Price" => "3.58", "Quantity" => "1"),
array("SKU" => "ythwq836", "Title" => "Frizbee", "Description" => "Whammo Frisbee Disc", "Price" => "2.47", "Quantity" => "2")
);
?>
<?php if (count($items) > 0): ?>
<table>
<thead>
<tr>
<th><?php echo implode('</th><th>', array_keys(current($items))); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($items as $row): array_map('htmlentities', $row); ?>
<tr>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php
$final = array_shift($items);
foreach (array_column as $key => &$value){
$value += array_sum(array_row($Price . $Quantity));
}
unset($value);
var_dump($final);
?>
我想获取每个项目的价格,将其乘以该数组中的数量并将总和添加到变量中,然后打印。我在这方面有点新,所以借口我的代码!
答案 0 :(得分:0)
将每个项目的价格合并到一个数组中,然后最后使用array_sum()
-
$eachPrice = array();
foreach ($items as $key => $val) {
$eachPrice[] = $val['Price'] * $val['Quantity'];
}
$totalPrice = array_sum($eachPrice);
var_dump($totalPrice); // should be total price of all items