Laravel刀片中的多个数组的总和

时间:2019-03-07 12:29:41

标签: javascript php arrays laravel-blade

我有5个数组,每个数组包含medicine_pricemedicine_quantity。我想要的是添加每个数组的结果。问题是,我无法获得每个数组的正确结果。

示例:

Array 1: result1 = medicine_price * medicine_quantity

Array 2: result2 = medicine_price * medicine_quantity

Array 3: result3 = medicine_price * medicine_quantity

Array 4: result4 = medicine_price * medicine_quantity

Array 5: result5 = medicine_price * medicine_quantity

total = result1 + result2 + result3 + result4 + result5

这应该是我想要的结果。而且由于我已经增加了它们,所以可能还会包含更多的数组。

代码:

@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

1 个答案:

答案 0 :(得分:0)

您可以这样做:

@php
    $totalPrice = 0;
@endphp

@foreach($carts as $key => $cart)

    {!! $cart->medicine_quantity !!}

    @php
        $value = $cart->medicine_price  * $cart->medicine_quantity;
        $totalPrice += $value;
    @endphp

    <input type='hidden' id='sample{{ $key + 1 }}' value='{{ $value }}'>

@endforeach

Total price: {{ $totalPrice }}

希望这会有所帮助!