Example:
$array = range(1, 2);
$map = Arrays::toMap($array, function ($elem) {
return $elem * 10;
}, function ($elem) {
return $elem + 1;
});
Result:
Array
(
[10] => 2
[20] => 3
)
public static function parse($data) { $array = array(); $items = Arrays::filterNotBlank(explode(',', $data)); foreach ($items as $item) { $elements = explode(';', $item); $media = Arrays::first($elements); $params = array_slice($elements, 1); list($type, $subtype) = Arrays::map(explode('/', $media), Functions::trim()); $q = Arrays::getValue(self::extractParams($params), 'q'); $array[] = array('type' => $type, 'subtype' => $subtype, 'q' => $q); } usort($array, '\\Ouzo\\Http\\AcceptHeaderParser::_compare'); return Arrays::toMap($array, function ($input) { return $input['type'] . '/' . $input['subtype']; }, function ($input) { return $input['q']; }); }
public function toMap($keyFunction, $valueFunction = null) { $this->_array = Arrays::toMap($this->_array, $keyFunction, $valueFunction); return $this; }
/** * @test */ public function shouldUseIdentityAsDefaultValueFunctionInToMap() { //given $array = range(1, 2); //when $map = Arrays::toMap($array, function ($elem) { return $elem * 10; }); //then $this->assertEquals(array(10 => 1, 20 => 2), $map); }