Oliver Spryn Oliver Spryn - 7 days ago 5
PHP Question

PHP Unset Array Value

I am working with a PHP loop, and I had one question regarding how unset affects the array keys. This array uses the standard numeric keys assigned by PHP,

0, 1, 2, 3 etc...
. Whenever
unset()
runs on an array value, are the array keys shuffled or are they maintained as before?

Thank you for your time.

Answer

The keys are not shuffled or renumbered. The unset() key is simply removed and the others remain.

$a = array(1,2,3,4,5);
unset($a[2]);
print_r($a);

Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [4] => 5
)