将集合中的值相乘

Multiply values in a Collection

大家好,我有这样的东西,我想乘以每个对象的 "number"。例如 2(第一个对象编号)x3(第二个对象编号)=6(答案应该是 6)有人知道怎么做吗?该数组并不总是 same.here 它有 2 个对象,但可以更改。

[{"id":7,"date":"2020-03-14","number":2},{"id":20,"date":"2020-03-15","number":3}]  

更新答案(有效)

                     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);
                        }

您可以使用Laravel collection

reduce()方法

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);