PHP 8.5.0 Alpha 1 available for testing

Voting

: max(two, one)?
(Example: nine)

The Note You're Voting On

macnimble at gmail dot com
16 years ago
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>';
?>

<< Back to user notes page

To Top