Skip to content

Instantly share code, notes, and snippets.

@stevedev
Created January 14, 2014 22:07
Show Gist options
  • Save stevedev/8426807 to your computer and use it in GitHub Desktop.
Save stevedev/8426807 to your computer and use it in GitHub Desktop.
Unsetting array elements safely in PHP.
<?php
$my_array = array( 'foo', 'bar', 'baz' );
// $key is index
foreach( $array as $key => $val ) {
if ($val == 'foo') {
unset( $key);
}
}
// => array( 'bar', 'baz' );
$my_other_array = array( 'foo' => 1, 'bar' => 2, 'baz' => 3 );
foreach( $my_other_array as $key => $val ) {
if ($key == 'foo') {
unset( $key);
}
}
// => array( 'bar' => 2, 'baz' => 3 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment