Skip to content

Instantly share code, notes, and snippets.

@MilesChou
Last active November 30, 2018 06:36
Show Gist options
  • Save MilesChou/b50ac37bad88d4ba1654e8a9b9ceeea4 to your computer and use it in GitHub Desktop.
Save MilesChou/b50ac37bad88d4ba1654e8a9b9ceeea4 to your computer and use it in GitHub Desktop.
<?php
$totalScore = 0;
foreach ($exam as $value) {
foreach ($value->mocks as $mock) {
foreach ($mock->items as $item) {
$totalScore += (int)$item->ext->score;
}
}
}
$totalScore = collect($exam)->flatMap->mocks->flatMap->items->reduce(function ($c, $v) {
$c += $v->ext->score;
return $c;
}, 0);
// ---------------------------------
$totalScore = 0;
foreach ($exam as $value) {
foreach ($value->mocks as $mock) {
foreach ($mock->items as $item) {
$totalScore += (int)$item->score;
}
}
}
$totalScore = collect($exam)->flatMap->mocks->flatMap->items->sum->score;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment