I played a bit with php's array_map function and thought about using it with named arguments, as the parameter order always irritates me. (The functions don't do something useful. Just needed something to test.)
So far so good, this is working as expected:
$testArray = [
'a' => [],
'b' => [1, 2, 3],
];
$result = array_map(array: $testArray, callback: fn (array $arr) => count($arr) === 0 ? null : $arr);
(Even if I needed a while to figure out the name of the array parameter, as it is documented as array1
in the german docs. But that's something, I'll look into as soon as I'm having time for it.)
But then I tried to call the function with more parameters.
So, my first try:
array_map(array: $testArray, callback: fn (array $arr) => count($arr) === 0 ? null : $arr, []);
Fatal error: Cannot use positional argument after named argument in
I learned that I must not pass positional arguments after named arguments.
Then the other way round:
array_map([], array: $testArray, callback: fn (array $arr) => count($arr) === 0 ? null : $arr);
Fatal error: Uncaught Error: Named parameter $callback overwrites previous argument in
Okay. It takes the first parameter as the first positional parameter and is confused, as I overwrite it.
I started googling. Okay, unknown named arguments are passed to variadic parameters. Let's try it:
array_map(array: $testArray, callback: fn (array $arr) => count($arr) === 0 ? null : $arr, additional: []);
Fatal error: Uncaught ArgumentCountError: array_map() does not accept unknown named parameters
So... is there any way to do this? I even visited the rfc about named arguments, but didn't find anything that looks like the solution. Can you guys give me a hint about, how to do this?