Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/helpers/array_helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions system/Helpers/array_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function array_sort_by_multiple_keys(array &$array, array $sortColumns): bool
* @param iterable<array-key, mixed> $array The multi-dimensional array
* @param string $id Something to initially prepend to the flattened keys
*
* @return array<string, mixed> The flattened array
* @return array<array-key, mixed> The flattened array
*/
function array_flatten_with_dots(iterable $array, string $id = ''): array
{
Expand All @@ -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;
}
Expand Down
37 changes: 35 additions & 2 deletions tests/system/Helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public static function provideSortByMultipleKeys(): iterable

/**
* @param array<array-key, mixed> $input
* @param array<string, mixed> $expected
* @param array<array-key, mixed> $expected
*/
#[DataProvider('provideArrayFlattening')]
public function testArrayFlattening(array $input, array $expected): void
Expand All @@ -418,7 +418,7 @@ public function testArrayFlattening(array $input, array $expected): void
}

/**
* @return iterable<string, array{array<array-key, mixed>, array<string, mixed>}>
* @return iterable<string, array{array<array-key, mixed>, array<array-key, mixed>}>
*/
public static function provideArrayFlattening(): iterable
{
Expand All @@ -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,
Expand Down
Loading