I have an array like this:
$array = [ 0 => 'Apple', 2 => 'Orange', 5 => 'Pear', 8 => 'Pear' ]
Is there a way to fill in the missing indexes with a default value (for example, an empty string or null)?
I’d like to insert new elements into the array at the following keys: 1, 3, 4, 6, 7
My result should be:
[ 0 => 'Apple', 1 => '', 2 => 'Orange', 3 => '', 4 => '', 5 => 'Pear', 6 => '', 7 => '', 8 => 'Pear' ]
This should be faster for larger arrays. For smaller arrays any method will do.