note 34749 added to function.array-flip
| From: | znailz at yahoo dot com | Date: | Tue, 05 Aug 2003 19:42:06 +0000 |
| Subject: | note 34749 added to function.array-flip | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-53449@lists.php.net to get a copy of this message | ||
I know a lot of people want a function to remove a key by value from an array. I saw solutions that
iterate(!) though the whole array comparing value by value and then unsetting that value's key.
PHP has a built-in function for pretty much everything (heard it will even cook you breakfast), so
if you think "wouldn't it be cool if PHP had a function to do that...", odds are it
already has. Check out this example. It takes a value, gets all keys for that value if it has
duplicates, unsets them all, and returns a reindexed array.
$arr = array(11,12,13,12); // sample array
$arr = array_flip($arr);
unset($arr[12]);
$arr = array(array_keys($arr));
$arr contains:
Array
(
[0] => Array
(
[0] => 11
[1] => 13
)
)
----
Manual Page -- http://www.php.net/manual/en/function.array-flip.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34749
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34749&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34749&report=yes