23

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.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
  • 11
    This is something you could have trivially tried out yourself... – Marc B Jun 16 '11 at 18:43
  • 3
    He could have tried himself, but now this question is cemented here which is important to for simple unset google searches. It definitely helped me, so I thank you Oliver. – Sweet Chilly Philly May 01 '17 at 00:44
  • 1
    @SweetChillyPhilly That's not the first time SO has taken the place of documentation/trivial examples. Glad it helped. – Oliver Spryn May 01 '17 at 16:30

6 Answers6

44

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
)
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • @Michael can we remove specific values from array like `4` with only single with `unset() ` – Rafee Jan 02 '12 at 11:39
  • 1
    @Rafee Do you mean to remove multiple values with one `unset()`? If so, you could use `array_diff()` to return a new array without the values you wanted removed. – Michael Berkowski Jan 02 '12 at 15:22
3

Test it yourself, but here's the output.

php -r '$a=array("a","b","c"); print_r($a); unset($a[1]); print_r($a);'
Array
(
    [0] => a
    [1] => b
    [2] => c
)
Array
(
    [0] => a
    [2] => c
)
Glen Solsberry
  • 11,960
  • 15
  • 69
  • 94
2

They are as they were. That one key is JUST DELETED

genesis
  • 50,477
  • 20
  • 96
  • 125
2

The Key Disappears, whether it is numeric or not. Try out the test script below.

<?php
    $t = array( 'a', 'b', 'c', 'd' );
    foreach($t as $k => $v)
        echo($k . ": " . $v . "<br/>");
    // Output: 0: a, 1: b, 2: c, 3: d

    unset($t[1]);

    foreach($t as $k => $v)
        echo($k . ": " . $v . "<br/>");
    // Output: 0: a, 2: c, 3: d
?>
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
1

This might be a little bit out of context but in unsetting values from a global array, apply the answer by Michael Berkowski above but in use with $GLOBALS instead of the the global value you declared with global $variable_name. So it will be something like:

unset($GLOBALS['variable_name']['array_key']);

Instead of:

global $variable_name; unset($variable_name['array_key']);

NB: This works only if you're using global variables.

David Lartey
  • 531
  • 8
  • 15
0

The keys are maintained with the removed key missing but they can be rearranged by doing this:

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

Outputs:

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

Notice that if we do the following without rearranging:

unset($array[2]);
$array[]=3;

The index of the value 3 will be 5 because it will be pushed to the end of the array and will not try to check or replace missing index. This is important to remember when using FOR LOOP with index access.

USER249
  • 1,080
  • 7
  • 14