HH\Vector::mapWithKey

Returns a Vector containing the values after an operation has been applied to each key and value in the current Vector.

Description

public function mapWithKey(
  (function(int,Tv):Tu) $callback,
): Vector<Tu>

Returns a Vector containing the values after an operation has been applied to each key and value in the current Vector.

Every key and value in the current Vector is affected by a call to mapWithKey(), unlike filterWithKey() where only values that meet a certain criteria are affected.

Parameters

  • (function(int,Tv):Tu) $callback - The callback containing the operation to apply to the Vector keys and values.

Return Values

  • Vector<Tu> - a Vector containing the values after a user-specified operation on the current Vector's keys and values is applied.

Examples

This example shows how mapWithKey can be used to create a new Vector based on $v's keys and values:

<?hh

namespace Hack\UserDocumentation\API\Examples\Vector\MapWithKey;

$v = Vector {'red', 'green', 'blue', 'yellow'};

$sentences = $v->mapWithKey(
  ($index, $color) ==> "Color at {$index}: {$color}",
);

echo implode("\n", $sentences)."\n";
Output
Color at 0: red
Color at 1: green
Color at 2: blue
Color at 3: yellow