Skip to content
Advertisement

How to plus values of multi arrays

enter image description here

Here is my code in blade Laravel:

@php
$serialize = array_map("serialize", $arrayplus);
$uniqueSerialize = array_unique($serialize);
$amountplus = array_map("unserialize", $uniqueSerialize);
dd($amountplus);
@endphp

When I use array_sum() function its return 0 I am trying to plus values of multi arrays

Advertisement

Answer

Try the below code.

@php
$serialize = array_map("serialize", $arrayplus);
$uniqueSerialize = array_unique($serialize);
$amountplus = array_map("unserialize", $uniqueSerialize);
    $sum = 0;
    @if(!empty($amountplus))
        @foreach($amountplus as $amount)
            $sum += $amount['amount'];
        @endforeach
    @endif
    echo $sum;

@endphp

and if you want to use array_sum then you need to do it as below.

$sum = array_sum(array_map(function($amountplus) { 
    return $amountplus['amount']; 
}, $amountplus));
echo $sum;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement