diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 39eddfb3664d..10b39dc92896 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -34,6 +34,7 @@ Release Date: Unreleased - **Database:** Fixed a bug where `SQLite3\Connection::_foreignKeyData()` did not escape table identifiers in `PRAGMA foreign_key_list`, causing syntax errors on table names containing hyphens or schemas. - **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters. - **Debug Toolbar:** Fixed a bug where `History::setFiles()` emitted a PHP warning when attempting to delete a debugbar history file that had already been removed. +- **Helpers:** Fixed a bug where `array_flatten_with_dots()` renumbered numeric keys when flattening nested arrays, potentially overwriting values. - **Helpers:** Fixed a bug in `ellipsize()` where `$position` was clamped after slicing the beginning of the string and where float `1.0` was not treated identically to integer `1`. - **Helpers:** Fixed a bug where `excerpt()` threw a `TypeError` when `$phrase` was not found in `$text` or when `$phrase` was `null` and `$radius` was an odd integer. When `$phrase` is not found, `excerpt()` now falls back to extracting an excerpt from the beginning of `$text`. - **Helpers:** Fixed a bug where `format_number()` with `$precision = 0` appended a trailing comma in locales using a comma as the decimal separator. diff --git a/docs/helpers/array_helper.md b/docs/helpers/array_helper.md index f76403b89ba6..6d71e8de6fd3 100644 --- a/docs/helpers/array_helper.md +++ b/docs/helpers/array_helper.md @@ -188,7 +188,7 @@ In the same way, the method can also handle an array of objects. In the example - **Returns**: The flattened array -This function flattens a multidimensional array to a single key-value array by using dots as separators for the keys. +This function flattens a multidimensional array to a single key-value array by using dots as separators for the keys. Numeric keys at the outer level are preserved without renumbering. ```php $arrayToFlatten = [ diff --git a/system/Helpers/array_helper.php b/system/Helpers/array_helper.php index c5ccde761544..89b941f0a820 100644 --- a/system/Helpers/array_helper.php +++ b/system/Helpers/array_helper.php @@ -131,7 +131,7 @@ function array_sort_by_multiple_keys(array &$array, array $sortColumns): bool * @param iterable $array The multi-dimensional array * @param string $id Something to initially prepend to the flattened keys * - * @return array The flattened array + * @return array The flattened array */ function array_flatten_with_dots(iterable $array, string $id = ''): array { @@ -141,7 +141,7 @@ function array_flatten_with_dots(iterable $array, string $id = ''): array $newKey = $id . $key; if (is_array($value) && $value !== []) { - $flattened = array_merge($flattened, array_flatten_with_dots($value, $newKey . '.')); + $flattened = array_replace($flattened, array_flatten_with_dots($value, $newKey . '.')); } else { $flattened[$newKey] = $value; } diff --git a/tests/system/Helpers/ArrayHelperTest.php b/tests/system/Helpers/ArrayHelperTest.php index e91d8e56cc8e..8a3e127fe901 100644 --- a/tests/system/Helpers/ArrayHelperTest.php +++ b/tests/system/Helpers/ArrayHelperTest.php @@ -409,7 +409,7 @@ public static function provideSortByMultipleKeys(): iterable /** * @param array $input - * @param array $expected + * @param array $expected */ #[DataProvider('provideArrayFlattening')] public function testArrayFlattening(array $input, array $expected): void @@ -418,7 +418,7 @@ public function testArrayFlattening(array $input, array $expected): void } /** - * @return iterable, array}> + * @return iterable, array}> */ public static function provideArrayFlattening(): iterable { @@ -439,6 +439,39 @@ public static function provideArrayFlattening(): iterable ], ]; + yield 'numeric-key-before-nested-array' => [ + [ + 5 => 'first', + 'nested' => ['name' => 'john'], + 0 => 'last', + ], + [ + 5 => 'first', + 'nested.name' => 'john', + 0 => 'last', + ], + ]; + + yield 'list-with-nested-array' => [ + ['first', ['name' => 'john'], 'last'], + [0 => 'first', '1.name' => 'john', 2 => 'last'], + ]; + + yield 'numeric-key-with-empty-array' => [ + [ + -2 => [], + 'nested' => ['name' => null], + 7 => false, + 'other' => ['active' => true], + ], + [ + -2 => [], + 'nested.name' => null, + 7 => false, + 'other.active' => true, + ], + ]; + yield 'many-levels' => [ [ 'foo' => 1,