note 61558 deleted from function.array-flip by danbrown
| From: | danbrown@php.net | Date: | Fri, 14 May 2010 15:03:55 +0000 |
| Subject: | note 61558 deleted from function.array-flip by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169250@lists.php.net to get a copy of this message | ||
Note Submitter: mikeb at tracersinfo dot com
----
Further deriving on benles -> crescentfreshpot, I think the following restatement of
array_invert() reads much easier and probably runs faster, too. It does yield the same results:
function array_invert($arr) {
$flipped = array();
foreach ( $arr as $k => $a ) {
# put the value in the key, with a throw-away value. dups are inherently avoided,
# though overwritten. not sure if prefixing with if ( !isset($flipped[$a][$k]) )
# would speed this up or slow it down. probably depends on quantity of dups.
$flipped[$a][$k] = NULL;
}
foreach ( $flipped as $k => $fl ) {
# now make the keys the values.
$flipped[$k] = array_keys($fl);
}
return $flipped;
}