php-src icon indicating copy to clipboard operation
php-src copied to clipboard

array_merge a key digit [1-9+] returning 0

Open ophian opened this issue 4 years ago • 2 comments

Description

The following code (being part of a bigger uri handling):

// $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

 $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.

Oh, I just learned I could use

$params = array_replace($prepend_params, $params);

giving the expected result. But I still don't know if that prior behaviour is intended to happen.

PHP Version

PHP 8.1.2

Operating System

Win7

ophian avatar Jan 22 '22 13:01 ophian

While admittedly confusing (as integers and strings aren't handled in the same way), it's not a bug.

https://www.php.net/manual/en/function.array-merge.php

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

To preserve numeric keys, you can use +.

var_dump(array_merge([1 => 'foo'], [2 => 'bar']));
// array(2) {
//   [0]=>
//   string(3) "foo"
//   [1]=>
//   string(3) "bar"
// }

var_dump([1 => 'foo'] + [2 => 'bar']);
// array(2) {
//   [1]=>
//   string(3) "foo"
//   [2]=>
//   string(3) "bar"
// }

iluuu1994 avatar Jan 22 '22 13:01 iluuu1994

@iluuu1994 Thanks for the heads up!

ophian avatar Jan 22 '22 14:01 ophian