I need to iterate over an array that looks like this:
$myArray = array( 'key1' => array('subkey' => 'subvalue'), 'key2' => array('subkey' => 'subvalue'), ) Into each nested associative array, I want to add a key-value pair based on the outside key, like this:
$myNewArray = array( 'key1' => array('subkey' => 'subvalue', 'newkey' => 'only for key1'), 'key2' => array('subkey' => 'subvalue'), ) Ideally, I'm looking for something like:
$myNewArray = array_map(function($key, $value) { if ($key == 'key1') { $value['newkey'] = 'only for key1'; } return $value; }, $myArray); However, that obviously doesn't work as callback isn't given two parameters but only one. I could do something like this:
$myNewArray = array_map(function($key, $value) { if ($key == 'key1') { $value['newkey'] = 'only for key1'; } return WHAT?? }, array_keys($myArray), $myArray); However, what do I return here? It seems to always construct a new array, i.e. discarding my string keys (key1 and key2), while a single-array array_map() keeps them.
I can use array_walk() but it has a rather strange API (flipped parameter order, array passed by reference etc.) so I would generally prefer if this could be achieved using array_map(). Can it?