17

I'm accessing an array by reference inside a foreach loop, but the unset() function doesn't seem to be working:

foreach ( $this->result['list'] as &$row ) {
    if ($this_row_is_boring) {
        unset($row);
    }
}

print_r($this->result['list']); // Includes rows I thought I unset

Ideas? Thanks!

LihO
  • 41,190
  • 11
  • 99
  • 167
Summer
  • 2,488
  • 3
  • 23
  • 32

3 Answers3

34

You're unsetting the reference (breaking the reference). You'd need to unset based on a key:

foreach ($this->result['list'] as $key => &$row) {
    if ($this_row_is_boring) {
        unset($this->result['list'][$key]);
    }
}
ircmaxell
  • 163,128
  • 34
  • 264
  • 314
  • 7
    Of note is that array indexes are preserved, even if you did not specify them (automatic numeric indexes) when the array was initialized. That is to say, it will not re-organize numeric array keys to be sequential. – amphetamachine Jun 16 '10 at 15:55
8
foreach ($this->result['list'] as $key => &$row) {
    if ($this_row_is_boring) {
        unset($this->result['list'][$key]);
    }
}
unset($row);

Remember: if you are using a foreach with a reference, you should use unset to dereference so that foreach doesn't copy the next one on top of it. More info

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • 1
    Ummm, if you're using a foreach with a reference, you don't need to unset it on each loop. PHP does that for you automatically. You should dereference it **after** the loop. – ircmaxell Jun 16 '10 at 16:08
  • That's true... it was a typo. – Cristian Jun 16 '10 at 16:27
5

A bit of an explanation to the answers above.

After unset($row) the variable $row is unset. That does not mean the data in $row is removed; the list also has an element pointing to $row.

It helps to think of variables as labels. A piece of data can have one or more labels, and unset removes that label but does not touch the actual data. If all labels are removed the data is automatically deleted.

extraneon
  • 23,575
  • 2
  • 47
  • 51