Inconsistent array keys after unset + merge + add value, versions 7.3 & 7.4

51 Views Asked by At

Code example:

<?php

$values = [];                          // start values

$list = ['one', 'two', 'three'];       // some list
$oneMore = 'four';                     // some element

unset($list[2]);                       // unset last(!) list element

$values = array_merge($values, $list); // add list to values
$values[] = $oneMore;                  // add element to values

print_r($values);

Result for 7.3.33:

Array
(
    [0] => one
    [1] => two
    [2] => four
)

Result for 7.4.29:

Array
(
    [0] => one
    [1] => two
    [3] => four
)

As you see, the keys are different in 7.3 and 7.4.

This happenes only when last element of $list array is unset. If some other element is unset, the result is identical.

Question: Is this expected/desired behaviour in 7.4+? Or a bug?

Note: If an empty array is added during merge, the resulting arrays will be indentical, i.e.:

$values = array_merge($values, $list, []);

https://sandbox.onlinephpfunctions.com/c/9625fee6-d127-45f1-8c68-3c64bcb87d0c

0

There are 0 best solutions below