0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CakePHPのCollectionのsumOfの用例

Posted at

$collection = new Collection($people);
$sumOfAges =  $collection->sumOf('age');

$sumOfChildrenAges = $collection->sumOf(function ($person) {
    return $person->child->age;
});

$sumOfDadAges = $collection->sumOf('dad.age');

こういう用例は載ってたけど、Collectionに対しても同じように使えるのか検証

public function test()
    {
        Log::debug(collection([
            [1,1,1,1,1],
            [2,2,2,2],
            [3,3,3]
        ])->sumOf(function (array $numberGroup)
        {
            return count($numberGroup);
        }));
    }

期待 5 + 4 + 3 = 12
結果 Debug: 12

要素数に対してもいけそうですね

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?