From 75d54000aee17788b6a5ee99dc238bddab8e9c90 Mon Sep 17 00:00:00 2001 From: grimpirate Date: Tue, 16 Jan 2024 18:50:02 -0500 Subject: [PATCH] DotArrayFilter replace rather than merge The use of array_merge_recursive rewrites numerically indexed values when attempting to retrieve validated data. Use of the array_replace_recursive function preserves their ordering. Unit test demonstrates the failure of array_merge_recursive's use rather than array_replace_recursive --- system/Validation/DotArrayFilter.php | 2 +- tests/system/Validation/DotArrayFilterTest.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/system/Validation/DotArrayFilter.php b/system/Validation/DotArrayFilter.php index 4f696886451d..1ba9b92fa710 100644 --- a/system/Validation/DotArrayFilter.php +++ b/system/Validation/DotArrayFilter.php @@ -44,7 +44,7 @@ public static function run(array $indexes, array $array): array $segments ); - $result = array_merge_recursive($result, self::filter($segments, $array)); + $result = array_replace_recursive($result, self::filter($segments, $array)); } return $result; diff --git a/tests/system/Validation/DotArrayFilterTest.php b/tests/system/Validation/DotArrayFilterTest.php index 6d3b380f517a..c5d5b8310841 100644 --- a/tests/system/Validation/DotArrayFilterTest.php +++ b/tests/system/Validation/DotArrayFilterTest.php @@ -180,4 +180,19 @@ public function testRunNestedArray() ]; $this->assertSame($expected, $result); } + + public function testRunReturnOrderedIndices() + { + $data = [ + 'foo' => [ + 2 => 'bar', + 0 => 'baz', + 1 => 'biz', + ], + ]; + + $result = DotArrayFilter::run(['foo.2', 'foo.0', 'foo.1'], $data); + + $this->assertSame($data, $result); + } }