Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?

https://bugs.php.net/bug.php?id=74077

Good news is that it isn't so difficult to explain this. What's happening is array_map really has two modes of operation: one array and multiple arrays.

Using one array, (a) with $callback: keep array keys, transform value using $callback(value) (b) without $callback: return a copy of the array

array_map(function($value) { return $value; }, $array)

Using more than one array, (a) with $callback: use numeric keys, create values using $callback(values...) (b) without $callback: use numeric keys, create values as array(values...)

array_map(function(...$values) { return $values; }, ...$arrays)

https://3v4l.org/oZjhV

https://github.com/php/php-src/blob/PHP-7.1.1/ext/standard/array.c#L5306

The difference between the two modes only really matters when using the default behavior - had array_map treated the callback as either $callback($one_value) or $callback(array $multiple_values) then the duality would be more obvious.

Taking that duality into account, I think the current behavior makes sense: with one array either transform it or don't change it at all, and with multiple arrays either transform them or "merge" them together. Consider that it would take conscious effort by a developer to use either mode, passing one array argument vs. passing multiple arrays (or using ... expansion), so accidentally getting one default behavior instead of the other is unlikely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment