Example:
$array = array('a1' => 1, 'a2' => 2, 'c' => 3);
$filtered = Arrays::filterByKeys($array, function ($elem) {
return $elem[0] == 'a';
});
Result:
Array
(
[a1] => 1
[b2] => 2
)
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))))); }); }
public function filterByKeys($function) { $this->_array = Arrays::filterByKeys($this->_array, $function); return $this; }
/** * @test */ public function shouldFilterByKeys() { //given $array = array('a1' => 1, 'a2' => 2, 'c' => 3); //when $filtered = Arrays::filterByKeys($array, function ($elem) { return $elem[0] == 'a'; }); //then $this->assertEquals(array('a1' => 1, 'a2' => 2), $filtered); }