note 93505 added to function.array-flip
| From: | (nemo@neminis.org) | Date: | Sun, 13 Sep 2009 21:45:44 +0000 |
| Subject: | note 93505 added to function.array-flip | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160519@lists.php.net to get a copy of this message | ||
There are two old notes (from 2005) at the bottom, that provide an alternative implementations of
array_flip that preserve duplicates.
Here there is another one, much simpler:
function assoc_array_flip($array) {
$flipped = array();
foreach ($array as $key => $value) {
$flipped[$value][] = $key;
}
return $flipped;
}
Example:
$test = array(1 => "London", 2 => "Naples", 3 => "London");
print_r($test);
$flipped_test = assoc_array_flip($test);
print_r($flipped_test);
Output:
Array
(
[1] => London
[2] => Naples
[3] => London
)
Array
(
[London] => Array
(
[0] => 1
[1] => 3
)
[Naples] => Array
(
[0] => 2
)
)
----
Server IP: 64.71.164.2
Probable Submitter: 86.133.238.20
----
Manual Page -- http://www.php.net/manual/en/function.array-flip.php
Edit -- https://master.php.net/note/edit/93505
Del: integrated -- https://master.php.net/note/delete/93505/integrated
Del: useless -- https://master.php.net/note/delete/93505/useless
Del: bad code -- https://master.php.net/note/delete/93505/bad+code
Del: spam -- https://master.php.net/note/delete/93505/spam
Del: non-english -- https://master.php.net/note/delete/93505/non-english
Del: in docs -- https://master.php.net/note/delete/93505/in+docs
Del: other reasons-- https://master.php.net/note/delete/93505
Reject -- https://master.php.net/note/reject/93505
Search -- https://master.php.net/manage/user-notes.php