note 89881 added to function.unset
| From: | macnimble at gmail dot com | Date: | Thu, 26 Mar 2009 22:39:29 +0000 |
| Subject: | note 89881 added to function.unset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152500@lists.php.net to get a copy of this message | ||
Two ways of unsetting values within an array:
<?php
# remove by key:
function array_remove_key ()
{
$args = func_get_args();
return array_diff_key($args[0],array_flip(array_slice($args,1)));
}
# remove by value:
function array_remove_value ()
{
$args = func_get_args();
return array_diff($args[0],array_slice($args,1));
}
$fruit_inventory = array(
'apples' => 52,
'bananas' => 78,
'peaches' => 'out of season',
'pears' => 'out of season',
'oranges' => 'no longer sold',
'carrots' => 15,
'beets' => 15,
);
echo "<pre>Original Array:\n",
print_r($fruit_inventory,TRUE),
'</pre>';
# For example, beets and carrots are not fruits...
$fruit_inventory = array_remove_key($fruit_inventory,
"beets",
"carrots");
echo "<pre>Array after key removal:\n",
print_r($fruit_inventory,TRUE),
'</pre>';
# Let's also remove 'out of season' and 'no longer sold' fruit...
$fruit_inventory = array_remove_value($fruit_inventory,
"out of season",
"no longer sold");
echo "<pre>Array after value removal:\n",
print_r($fruit_inventory,TRUE),
'</pre>';
?>
----
Server IP: 69.147.83.197
Probable Submitter: 72.192.19.244
----
Manual Page -- http://www.php.net/manual/en/function.unset.php
Edit -- https://master.php.net/note/edit/89881
Del: integrated -- https://master.php.net/note/delete/89881/integrated
Del: useless -- https://master.php.net/note/delete/89881/useless
Del: bad code -- https://master.php.net/note/delete/89881/bad+code
Del: spam -- https://master.php.net/note/delete/89881/spam
Del: non-english -- https://master.php.net/note/delete/89881/non-english
Del: in docs -- https://master.php.net/note/delete/89881/in+docs
Del: other reasons-- https://master.php.net/note/delete/89881
Reject -- https://master.php.net/note/reject/89881
Search -- https://master.php.net/manage/user-notes.php