Invokes the function for each key in the array. Creates a new array containing the keys returned by the function.
Example:
$array = array(
'k1' => 'v1',
'k2' => 'v2',
'k3' => 'v3'
);
$arrayWithNewKeys = Arrays::mapKeys($array, function ($key) {
return 'new_' . $key;
});
Result:
Array
(
[new_k1] => v1
[new_k2] => v2
[new_k3] => v3
)
/** * @test */ public function shouldMapKeys() { //given $array = array('k1' => 'v1', 'k2' => 'v2', 'k3' => 'v3'); //when $arrayWithNewKeys = Arrays::mapKeys($array, function ($key) { return 'new_' . $key; }); //then $this->assertEquals(array('new_k1' => 'v1', 'new_k2' => 'v2', 'new_k3' => 'v3'), $arrayWithNewKeys); }
public function mapKeys($function) { $this->_array = Arrays::mapKeys($this->_array, $function); return $this; }
public function getRequestHeaders() { $headers = Arrays::filterByKeys($_SERVER, Functions::startsWith('HTTP_')); return Arrays::mapKeys($headers, function ($key) { return str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5))))); }); }