note 39523 added to ref.array
| From: | php-general at lists dot php dot net | Date: | Sat, 31 Jan 2004 19:54:13 +0000 |
| Subject: | note 39523 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64485@lists.php.net to get a copy of this message | ||
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 )
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39523
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39523&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39523&report=yes
Search -- http://master.php.net/manage/user-notes.php