Hi all I have something like this and i want to multiply each object's "number". for example 2(first object number)x3(second object number)=6 (answer should be 6) does anyone know how to do it? The array is not always same.here it has 2 objects but it can be changed.
[{"id":7,"date":"2020-03-14","number":2},{"id":20,"date":"2020-03-15","number":3}]
Updated answer(Working)
foreach ($studentDetail as $student){
$number = calendar::where('student_id','=',$student['id'])
->where('date','>=',$jsonData->checkin)
->where('date','<',$jsonData->checkout)
->get();
Log::info($number );
$multiplied= $number ->reduce(function ($carry, $item) {
return $carry * $item->number;
}, 1);
Log::info($multiplied);
}
You can use reduce()
method of Laravel collection
The reduce method reduces the collection to a single value, passing the result of each iteration into the subsequent iteration
$multiplied= $collection->reduce(function ($carry, $item) {
return $carry * $item->number;
}, 1);