note 74171 added to function.array-keys
| From: | RQuadling at GMail dot com | Date: | Wed, 28 Mar 2007 13:31:58 +0000 |
| Subject: | note 74171 added to function.array-keys | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124881@lists.php.net to get a copy of this message | ||
If you want to remove a value from an array, then there is no direct mechanism.
The following function uses the array_keys() function to find the key(s) of the value that you want
to remove and then removes the elements for that key.
I've also given some examples and the output.
<?php
/**
* array array_remove ( array input, mixed search_value [, bool strict] )
**/
function array_remove(array &$a_Input, $m_SearchValue, $b_Strict = False) {
$a_Keys = array_keys($a_Input, $m_SearchValue, $b_Strict);
foreach($a_Keys as $s_Key) {
unset($a_Input[$s_Key]);
}
return $a_Input;
}
?>
Beside scalar variables (integers, floats, strings, boolean), you can also use arrays as the values
you want to remove.
<?php
// Results in array(8, 8.0, '8', '8.0')
array_remove(array(8, 8.0, '8', '8.0', array(8), array('8')),
array(8));
// Results in array(8, 8.0, '8', '8.0', array('8'))
array_remove(array(8, 8.0, '8', '8.0', array(8), array('8')),
array(8), True);
?>
----
Server IP: 87.124.64.96
Probable Submitter: 83.104.121.66 (proxied: 1.1 BANDMAIL)
----
Manual Page -- http://www.php.net/manual/en/function.array-keys.php
Edit -- https://master.php.net/note/edit/74171
Del: integrated -- https://master.php.net/note/delete/74171/integrated
Del: useless -- https://master.php.net/note/delete/74171/useless
Del: bad code -- https://master.php.net/note/delete/74171/bad+code
Del: spam -- https://master.php.net/note/delete/74171/spam
Del: non-english -- https://master.php.net/note/delete/74171/non-english
Del: in docs -- https://master.php.net/note/delete/74171/in+docs
Del: other reasons-- https://master.php.net/note/delete/74171
Reject -- https://master.php.net/note/reject/74171
Search -- https://master.php.net/manage/user-notes.php