Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Automatic Index In Non Empty Array

When starting from an array whose maximum key is integer and negative, PHP used to continue assigning indices with 0, instead of the following negative number. It is fixed in PHP 8.0.

PHP code

<?php

$array = [
    -10 => 'a',
];
$array[] = 'b';

print_r($array);

?>

Before

Array
(
    [-10] => a
    [0] => b
)

After

Array
(
    [-10] => a
    [-9] => b
)

PHP version change

This behavior changed in 8.0.

See Also

Extension

© 2023-2026, Damien Seguy - Exakat