note 39523 deleted from ref.array by aidan
| From: | aidan@php.net | Date: | Sun, 09 Oct 2005 09:15:20 +0000 |
| Subject: | note 39523 deleted from ref.array by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-96743@lists.php.net to get a copy of this message | ||
Note Submitter:
----
unset($bar['mushroomsoup']) only works it the key
is 'mushroomsoup'.If you want to erase elements
of an array identified by values rather than by keys
you can use this function:
<?
function unset_by_val($needle,&$haystack) {
# removes all entries in array $haystack,
# who's value is $needle
while(($gotcha = array_search($needle,$haystack)) > -1)
unset($haystack[$gotcha]);
}
$ring = array('gollum','smeagol','gollum','gandalf',
'deagol','gandalf');
print_r($ring); echo "<br>";
unset_by_val('gollum',$ring);
print_r($ring);
?>
Will output:
Array ( [0] => gollum [1] => smeagol [2] => gollum
[3] => gandalf [4] => deagol [5] => gandalf )
Array ( [1] => smeagol [3] => gandalf
[4] => deagol [5] => gandalf )