Skip to content

Conversation

@david-valdivia
Copy link
Contributor

This PR adds an Arr::mapKeys() method to transform array keys while preserving values.

Common use case: transforming API response keys from snake_case to camelCase.

// Transform keys
$data = ['first_name' => 'Taylor', 'last_name' => 'Otwell'];
$result = Arr::mapKeys($data, fn($key) => Str::camel($key));
// ['firstName' => 'Taylor', 'lastName' => 'Otwell']

// Prefix keys (My favorite)
$config = ['host' => 'localhost', 'port' => 3306];
$result = Arr::mapKeys($config, fn($key) => "db_{$key}");
// ['db_host' => 'localhost', 'db_port' => 3306]

The implementation follows the existing Arr::map() pattern, supporting callbacks with optional parameters (key only, or key with value).

@taylorotwell
Copy link
Member

You can just use mapWithKeys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants