title: Why can't I unset an array item passed by reference in a foreach loop?
body: <p>I have an array like</p>
  
  <pre><code>[0] Bert14:50
  [1] hello
  [2] Sarah14:50
  [3] bye
  [4] Dennis14:50
  [5] hi
  [6] wow
  </code></pre>
  
  <p>I want to reduce it to look like: </p>
  
  <pre><code>[0] Bert14:50
  [1] Sarah14:50
  [2] Dennis14:50
  </code></pre>
  
  <p>I've achieved that with this code:</p>
  
  <pre><code>//Doesn't any number exist in the array item? Then remove item
  //by first setting it to NULL, and after the loop do some reindexing etc.
  foreach($new_str as $item_key =&gt; &amp;$item) {
      if (!preg_match('~[0-9]+~', $item)) {
          $item = null;       
      }
  }
  //Remove null by using unique array...
  $new_str = array_values(array_unique($new_str));
  //..and then remove first item if it's null
  if ($new_str[0] === null) {unset($new_str[0]);}
  </code></pre>
  
  <p>But why does not this code do not remove items that does not contain 0-9? 
  Why can't I unset a passed by reference value like this?</p>
  
  <pre><code>foreach($new_str as $item_key =&gt; &amp;$item) {
      if (!preg_match('~[0-9]+~', $item)) {
         //Nothing seems to happen here. Output of $new_str is same as original 
         //array    
         unset($item); 
      }
  }
  </code></pre>