Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

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)

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
You can’t perform that action at this time.