Skip to content

Commit cfdbdcb

Browse files
[12.x] Add Arr::mapKeys() method
1 parent 40506d8 commit cfdbdcb

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/Illuminate/Collections/Arr.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,27 @@ public static function map(array $array, callable $callback)
793793
return array_combine($keys, $items);
794794
}
795795

796+
/**
797+
* Map over an array's keys.
798+
*
799+
* @param array $array
800+
* @param callable $callback
801+
* @return array
802+
*/
803+
public static function mapKeys(array $array, callable $callback): array
804+
{
805+
$keys = array_keys($array);
806+
$values = array_values($array);
807+
808+
try {
809+
$newKeys = array_map($callback, $keys, $values);
810+
} catch (ArgumentCountError) {
811+
$newKeys = array_map($callback, $keys);
812+
}
813+
814+
return array_combine($newKeys, $values);
815+
}
816+
796817
/**
797818
* Run an associative map over each of the items.
798819
*

tests/Support/SupportArrTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Collection;
99
use Illuminate\Support\ItemNotFoundException;
1010
use Illuminate\Support\MultipleItemsFoundException;
11+
use Illuminate\Support\Str;
1112
use InvalidArgumentException;
1213
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1314
use PHPUnit\Framework\TestCase;
@@ -993,6 +994,37 @@ public function testMap()
993994
$this->assertEquals(['first' => 'taylor', 'last' => 'otwell'], $data);
994995
}
995996

997+
public function testMapKeys()
998+
{
999+
// Basic transformation
1000+
$array = ['first_name' => 'Taylor', 'last_name' => 'Otwell'];
1001+
$result = Arr::mapKeys($array, fn($key) => Str::camel($key));
1002+
$this->assertEquals(['firstName' => 'Taylor', 'lastName' => 'Otwell'], $result);
1003+
1004+
// With value parameter
1005+
$array = ['name' => 'Taylor', 'age' => 31];
1006+
$result = Arr::mapKeys($array, fn($key, $value) => is_numeric($value) ? "num_{$key}" : $key);
1007+
$this->assertEquals(['name' => 'Taylor', 'num_age' => 31], $result);
1008+
1009+
// Key collision - last wins
1010+
$array = ['name' => 'First', 'NAME' => 'Second'];
1011+
$result = Arr::mapKeys($array, fn($key) => strtolower($key));
1012+
$this->assertEquals(['name' => 'Second'], $result);
1013+
1014+
// Empty array
1015+
$this->assertEquals([], Arr::mapKeys([], fn($key) => $key));
1016+
1017+
// Numeric keys
1018+
$array = [1, 2, 3];
1019+
$result = Arr::mapKeys($array, fn($key) => "item_{$key}");
1020+
$this->assertEquals(['item_0' => 1, 'item_1' => 2, 'item_2' => 3], $result);
1021+
1022+
// Prefix example
1023+
$config = ['host' => 'localhost', 'port' => 3306, 'database' => 'app'];
1024+
$result = Arr::mapKeys($config, fn($key) => "db_{$key}");
1025+
$this->assertEquals(['db_host' => 'localhost', 'db_port' => 3306, 'db_database' => 'app'], $result);
1026+
}
1027+
9961028
public function testMapWithEmptyArray()
9971029
{
9981030
$mapped = Arr::map([], static function ($value, $key) {

0 commit comments

Comments
 (0)