我有 5 个数组,每个数组包含medicine_price, medicine_quantity。
我想要的是将每个数组的结果相加。
问题是,我无法获得每个数组的正确结果。
例子:
result1 = medicine_price * medicine_quantity
result2 = medicine_price * medicine_quantity
result3 = medicine_price * medicine_quantity
result4 = medicine_price * medicine_quantity
result5 = medicine_price * medicine_quantity
total = result1 + result2 + result3 + result4 + result5
Run Code Online (Sandbox Code Playgroud)
这应该是我想要的结果。
而且也可能会包含更多数组,因为我已经增加了它们。
代码 :
@php
$i = 1;
@endphp
@foreach($carts as $cart)
{!! $cart->medicine_quantity !!}
@php
$value= $cart->medicine_price * $cart->medicine_quantity;
echo "<input type='hidden' id='sample$i' value='$value'>";
$i++
@endphp
@endforeach
@php
echo "$Total_price"
@endphp
Run Code Online (Sandbox Code Playgroud) <td colspan="3">
<p class="text-left">
<h5>Discount ( @php echo "- $subtotalquant"; @endphp )</h5>
</p>
</td>
<td>
<p class="text-right">
<h5>
@php
echo"₱$subtotal";
@endphp
</h5>
</p>
</td>
Run Code Online (Sandbox Code Playgroud)
我想将这些值四舍五入到小数点后两位。但是数字格式似乎不起作用。
$subtotal = 40.608
$subtotalquant = 10.152
Run Code Online (Sandbox Code Playgroud)