From eae257862c3df9dc8ff55e846c8583d60492e94e Mon Sep 17 00:00:00 2001 From: Lennart Dommer Date: Sat, 26 Apr 2025 17:48:01 +0200 Subject: [PATCH 1/2] add append keys with arr helper --- src/Illuminate/Collections/Arr.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index e4ef759c8f6..7578d0657af 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -510,6 +510,18 @@ public static function prependKeysWith($array, $prependWith) return static::mapWithKeys($array, fn ($item, $key) => [$prependWith.$key => $item]); } + /** + * Append the key names of an associative array. + * + * @param array $array + * @param string $appendWith + * @return array + */ + public static function appendKeysWith($array, $appendWith) + { + return static::mapWithKeys($array, fn ($item, $key) => [$key.$appendWith => $item]); + } + /** * Get a subset of the items from the given array. * From 4795b12349f450e8213da92b09d43a6c999a8663 Mon Sep 17 00:00:00 2001 From: Lennart Dommer Date: Sat, 26 Apr 2025 21:37:57 +0200 Subject: [PATCH 2/2] add append keys with arr helper test --- tests/Support/SupportArrTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 85611982475..41dc107aefd 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -1499,6 +1499,27 @@ public function testPrependKeysWith() ], Arr::prependKeysWith($array, 'test.')); } + public function testAppendKeysWith() + { + $array = [ + 'id' => 1, + 'name' => 'Lennart', + 'skills' => ['php', 'mysql', 'laravel'], + 'meta' => [ + 'prs' => 1, + ], + ]; + + $this->assertEquals([ + 'id_user' => 1, + 'name_user' => 'Lennart', + 'skills_user' => ['php', 'mysql', 'laravel'], + 'meta_user' => [ + 'prs' => 1, + ], + ], Arr::appendKeysWith($array, appendWith: '_user')); + } + public function testTake(): void { $array = [1, 2, 3, 4, 5, 6];