Repository: twigphp/Twig (php)
Issue: Incorrect key mapping in twig arrays with integer keys
Hi!

I encountered an issue where twig modifdy some numeric keys in an associative array defined in my template.

### How to reproduce

In my template:

```twig
{% 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'} %}
{% dump({trad: trad, trad2: trad2}) %}
```

Expected output:

```php
array:2 [▼
  "trad" => array:6 [▼
    194 => "ABC"
    141 => "DEF"
    100 => "GHI"
    170 => "JKL"
    110 => "MNO"
    111 => "PQR"
  ]
  "trad2" => array:6 [▼
    194 => "ABC"
    141 => "DEF"
    100 => "GHI"
    170 => "JKL"
    110 => "MNO"
    111 => "PQR"
  ]
]
```

Real output:

```php
array:2 [▼
  "trad" => array:6 [▼
    194 => "ABC"
    141 => "DEF"
    100 => "GHI"
    170 => "JKL"
    110 => "MNO"
    195 => "PQR" // The key 111 is changed to 195 ¯\_(ツ)_/¯
  ]
  "trad2" => array:6 [▼
    194 => "ABC"
    141 => "DEF"
    100 => "GHI"
    170 => "JKL"
    110 => "MNO"
    111 => "PQR" // When the keys are explicitly defined as strings, they remain correct
                 // ... as integer... not following the PHP logical... but why not
                 // === EDIT === Sorry, this is "ok", PHP does that.
  ]
]
```

### Expected Behavior

Numeric keys should remain unchanged and not be altered by Twig when converted to PHP arrays. => this is the issue

String keys maybe should keep they type when converted to PHP arrays ? => this is a simple question ^^,

### Environment

* Twig versions: at least 3.15, still in 3.19
* PHP version: 8.3.14
* Framework : Symfony 6.4
Pull request description: Fix wrong array index
Fixes #4563


Patch:
diff --git a/CHANGELOG b/CHANGELOG
index d2682d6b..93264d3e 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -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)
diff --git a/src/Node/Expression/ArrayExpression.php b/src/Node/Expression/ArrayExpression.php
index 87d5cb8d..20c4c7bc 100644
--- a/src/Node/Expression/ArrayExpression.php
+++ b/src/Node/Expression/ArrayExpression.php
@@ -112,9 +112,6 @@ class ArrayExpression extends AbstractExpression
                 }
 
                 if ($nextIndex !== $key) {
-                    if (\is_int($key)) {
-                        $nextIndex = $key + 1;
-                    }
                     $compiler
                         ->subcompile($pair['key'])
                         ->raw(' => ')


Test patch:
diff --git a/tests/Fixtures/expressions/array.test b/tests/Fixtures/expressions/array.test
index 7d1c3b0a..eb7f400d 100644
--- a/tests/Fixtures/expressions/array.test
+++ b/tests/Fixtures/expressions/array.test
@@ -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();
@@ -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--
@@ -117,3 +124,5 @@ bar
 ok
 ok
 ok
+
+OK