Skip to content

Commit 53a68fe

Browse files
authored
Improve Collection::reduce docs (#10282)
* Document that keys are always passed, regardless of array type. * Fix incorrect and broken type * Give initial value to satisfy `int` type in strict setting
1 parent 379d9a5 commit 53a68fe

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

collections.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,7 @@ $collection->reduce(function (int $carry, int $item) {
24022402
// 10
24032403
```
24042404

2405-
The `reduce` method also passes array keys in associative collections to the given callback:
2405+
The `reduce` method also passes array keys to the given callback:
24062406

24072407
```php
24082408
$collection = collect([
@@ -2417,9 +2417,9 @@ $ratio = [
24172417
'eur' => 1.22,
24182418
];
24192419

2420-
$collection->reduce(function (int $carry, int $value, int $key) use ($ratio) {
2420+
$collection->reduce(function (int $carry, int $value, string $key) use ($ratio) {
24212421
return $carry + ($value * $ratio[$key]);
2422-
});
2422+
}, 0);
24232423

24242424
// 4264
24252425
```

0 commit comments

Comments
 (0)