Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

untymage's avatar

Passing PHP functions into ->map() method in collection ?

With pure php we can just pass the function as string into array_map like:

array_map('strtolower', ['Alex', 'JOHN']);

How can i do the exact same think for collection's map ?

collect(['Alex', 'JOHN'])->map('strtolower');

I did that but i get this error :

ArgumentCountError : strtolower() expects exactly 1 argument, 2 given
0 likes
2 replies
neilstee's avatar

@untymage try this:

collect(array_map('strtolower', ['Alex', 'JOHN']));

But if you really want to array map after collect:

collect(['Alex', 'JOHN'])->map(function($item){
	return array_map('strtolower', [$item]);
})->flatten();
1 like

Please or to participate in this conversation.