Cannot add element to the array as the next element is already occupied

Description

This error appears when the largest PHP integer has been used as an index in an array. When appending a new item in the array, PHP calculates the largest index, and adds one. Though, in this situation, it is not possible anymore to create a new integer, since the last one was used.

This error appears when a value was stored out of the way, yet the array is appended later.

Example

<?php

$array = [0 => 'a', PHP_INT_MAX => 'b'];
$array[] = 3;

?>

Solutions

  • Avoid using PHP_INT_MAX as index in the array.

  • Assign the value with an explicit index, rather than an append.