3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = <<<'EOT' <? $array = [ 'KEY_1' => 'VALUE_1', 'KEY_2' => 'VALUE_2', 'KEY_3' => 'VALUE_3', ]; EOT; $array = []; $key = null; $state = 0; foreach(token_get_all($str) as $token) { if(isset($token[0])) { if($token[0] === T_DOUBLE_ARROW) { $state = 1; } else if($token[0] === T_CONSTANT_ENCAPSED_STRING) { if($state === 0) { $key = substr($token[1], 1, -1); $state = 1; } else { $array[$key] = substr($token[1], 1, -1); $state = 0; } } } } var_dump($array);
Output for 7.2.29 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.32, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array(0) { }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array(0) { }
Output for 7.1.25, 7.2.0 - 7.2.13, 7.3.0, 7.3.32 - 7.3.33, 7.4.33, 8.0.13
array(3) { ["KEY_1"]=> string(7) "VALUE_1" ["KEY_2"]=> string(7) "VALUE_2" ["KEY_3"]=> string(7) "VALUE_3" }

preferences:
148.54 ms | 402 KiB | 152 Q