[php-src] Issue #7988: array_merge a key digit [1-9+] returning 0
| From: | iluuu1994 | Date: | Sat, 22 Jan 2022 13:42:54 +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-239215@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7988
Comment Author: iluuu1994
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
+.
```php
var_dump(array_merge([1 => 'foo'], [2 => 'foo']));
// array(2) {
// [0]=>
// string(3) "foo"
// [1]=>
// string(3) "foo"
// }
var_dump([1 => 'foo'] + [2 => 'foo']);
// array(2) {
// [1]=>
// string(3) "foo"
// [2]=>
// string(3) "foo"
// }
```