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

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.

1 participant