Laravel mapWithKeys collection

I am trying to create an array using the laravel collection function called mapWithKeys , but I could not achieve what I needed.

Here is my code

$years = range(1900, date('Y')); return collect($years)->mapWithKeys(function($value){ return [$value => $value]; })->all(); 

Expected Result

 Array ( [1900] => 1900 [1901] => 1901 [1902] => 1902 .... [2017] => 2017 ) 

But I get

 Array ( [0] => 1900 [1] => 1901 [2] => 1902 ... [117] => 2017 ) 
+5
source share
1 answer

I tested this code and it works great:

 $years = range(1900, date('Y')); return collect($years)->map(function($i) { return ['year' => $i]; }, $years)->pluck('year', 'year'); 
+2
source

Source: https://habr.com/ru/post/1263670/


All Articles