[php-src] Issue #7988: array_merge a key digit [1-9+] returning 0

From: Date: Sat, 22 Jan 2022 13:33:10 +0000
Subject: [php-src] Issue #7988: array_merge a key digit [1-9+] returning 0
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-239214@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/7988 Author: ophian ### Description The following code (being part of a bigger uri handling): ```php // $uri = '/archives/2017/06/P1/summary.html'; // $uri args $_args = array ( 0 => 'archives', 1 => '2017', 2 => '06', 3 => 'P1', 4 => 'summary', ); // $params $params = array ( 'P1' => true, 'summary' => true, ); /* We must always *assume* that Year, Month and Day are the first 3 arguments */ list(, $year, $month, $day) = $_args; // keep empty param, is 'archives' if (isset($year)) { $prepend_params[$year] = true; } if (isset($month)) { $prepend_params[$month] = true; } if (isset($day)) { $prepend_params[$day] = true; } var_dump($prepend_params); var_dump($params); var_export($prepend_params); var_export($params); $params = array_merge($prepend_params, $params); print_r($params); ``` Resulted in this output: ``` ouput var_dump(s) array(3) { [2017]=> bool(true) ["06"]=> bool(true) ["P1"]=> bool(true) } array(2) { ["P1"]=> bool(true) ["summary"]=> bool(true) } output var_export(s) array ( 2017 => true, '06' => true, 'P1' => true, ) array ( 'P1' => true, 'summary' => true, ) // output print_r with combined arrays Array ( [0] => 1 [06] => 1 [P1] => 1 [summary] => 1 ) ``` But I expected this output instead: ``` expected first key [2017] ``` I can even remove the list var generation and do a simple ```php $year = '2017'; $month = '06'; ``` with the same result for the merged year key. If the $_ args value or the $year var key is 02017 for example the merge will success. All nums [1-9+] turn into 0. So I asume the internal PHP num key regex returns it to be the 0 key and this is somehow the "wanted" behaviour. I currently don't see a way to cast this to an assoc string, without using weird workarounds or having to rewite the whole. ### PHP Version PHP 8.1.2 ### Operating System Win7

« previous php.bugs (#239214) next »