-1

I am workign with a Laravel collection and given this code:

 use Illuminate\Support\Collection;

 $input = ['1234', 42, '12Beers'];
 $result = (new Collection($input))->each(static function($item) {
     return (int) $item;}
 )->toArray();

I'd expect the result to look like:

[
  0 => 1234
  1 => 42
  2 => 12
]

but I'm getting:

 [
  0 => "1234"
  1 => 42
  2 => "12Beers"
]

It's as if the each is not being applied. What am I doing wrong?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347

1 Answers1

0

I wanted to use Collection::map. each is a foreach loop alternative in a sense that it doesn't change the state of the collection itself / it doesn't return a new collection with the result items.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347