Using Multiple Arrays with array_map in PHP
Today I stumble upon some code that I did not realise was possible with an
array_map function in PHP.
You are able to supply multiple arrays, which in turn will get ‘zipped up’ and passed to the supplied callback as parameters.
This allows for some rather elegant solutions to the ‘index issue’ and accessing keys from within the callback - both of which are easily achieved in an impertivive mindset.
range function we are able to supply a sequence of indexes based on the collection.
This allows us to access the current index from within the callback function.
One use-case that a
foreach provides is the ability to destructure an associative array, allowing you to access the key and value.
In its basic form an
array_map seems to be missing this capability, however, with the ability to supply multiple arrays this is not the case.