Skip to content

fix: preserve numeric keys when flattening arrays - #10826

Open
MohamedRF wants to merge 1 commit into
codeigniter4:developfrom
MohamedRF:fix/array-flatten-numeric-keys
Open

MohamedRF wants to merge 1 commit into
codeigniter4:developfrom
MohamedRF:fix/array-flatten-numeric-keys

Conversation

@MohamedRF

Copy link
Copy Markdown
Contributor

Description
array_flatten_with_dots() used array_merge() when adding flattened nested arrays. This renumbered numeric keys already in the result and could cause data loss. For example, [5 => 'first', 'nested' => ['name' => 'john'], 0 => 'last'] lost 'first' because its key became 0 and was subsequently overwritten.

Use array_replace() to retain numeric keys while preserving the existing replacement behavior for string keys. Add three regression cases for sparse keys, a mixed list, and negative keys with empty arrays and falsey values. Update PHPDoc, the user guide, and the 4.7.7 changelog.

Validation

  • Before the fix: two new regression cases failed, demonstrating key renumbering and value loss.
  • vendor/bin/phpunit --no-coverage tests/system/Helpers/ArrayHelperTest.php: 42 tests, 56 assertions passed on PHP 8.5.12.
  • File-scoped PHPStan: no errors in the changed helper and test file.
  • File-scoped PHP CS Fixer checks and git diff --check: passed.
  • Coverage was not measured. The full test suite, PHP version matrix, and remaining repository-wide checks are left to CI.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Use array_replace() to preserve numeric keys when merging flattened nested arrays. Renumbering could overwrite an earlier value when a later input reused the resulting numeric key. Add regression cases and update the helper documentation and changelog.
@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 24, 2026

@Ribeiro-MD Ribeiro-MD left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, minimal fix. The bug comes from array_merge() renumbering integer keys in both arrays, so any top-level numeric key already in $flattened got reindexed as soon as a nested array was merged in. The recursive call always returns string keys ("1.name", etc.), so array_replace() is the right tool: it keeps existing keys and order, appends new ones, and for string-key collisions it still lets the later value win, same as before. The three new provider cases (non-sequential keys, a list with a nested array, negative keys with an empty array) cover the interesting shapes well, and the array<array-key, mixed> PHPDoc update matches the real return type.

A couple of small thoughts:

This changes output for existing callers. Someone who relied on the old reindexing (e.g. treating the result of flattening a list as a fresh 0..n list) will now see the original keys. It's clearly a bug fix, but it might deserve a line under the changelog's behaviour-change / BC notes rather than only under bug fixes, so upgrades aren't surprised.

It may be worth one case with a non-empty $id prefix and a numeric top-level key, e.g. array_flatten_with_dots([3 => ['a' => 1], 4 => 'x'], 'p.'), to lock in that prefixed numeric keys come out as 'p.3.a' / 'p.4' strings and aren't affected by the change.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants