Skip to content

Commit

Permalink
bug #4565 Fix wrong array index (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Fix wrong array index

Fixes #4563

Commits
-------

b039999 Fix wrong array index
  • Loading branch information
fabpot committed Feb 1, 2025
2 parents a5ebdfe + b039999 commit fff5638
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 3.20.0 (2025-XX-XX)

* Fix wrong array index
* Bump minimum PHP version to 8.1

# 3.19.0 (2025-01-28)
Expand Down
3 changes: 0 additions & 3 deletions src/Node/Expression/ArrayExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ public function compile(Compiler $compiler): void
}

if ($nextIndex !== $key) {
if (\is_int($key)) {
$nextIndex = $key + 1;
}
$compiler
->subcompile($pair['key'])
->raw(' => ')
Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/expressions/array.test
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ Twig supports array notation
{{ does_not_exist[0]|default('ok') }}
{{ does_not_exist[0].does_not_exist_either|default('ok') }}
{{ does_not_exist[0]['does_not_exist_either']|default('ok') }}

{# indexes are kept #}
{% set trad = {194:'ABC',141:'DEF',100:'GHI',170:'JKL',110:'MNO',111:'PQR'} %}
{% set trad2 = {'194':'ABC','141':'DEF','100':'GHI','170':'JKL','110':'MNO','111':'PQR'} %}
{{ trad == trad2 ? 'OK' : 'KO' }}
--DATA--
$objectStorage = new SplObjectStorage();
$object = new stdClass();
Expand Down Expand Up @@ -84,6 +89,8 @@ bar
ok
ok
ok

OK
--DATA--
return ['bar' => 'bar', 'foo' => ['bar' => 'bar'], 'array_access' => new \ArrayObject(['a' => 'b']), 'object' => new stdClass()]
--CONFIG--
Expand Down Expand Up @@ -117,3 +124,5 @@ bar
ok
ok
ok

OK

0 comments on commit fff5638

Please sign in to comment.