note 97900 added to function.array-flip
| From: | Anonymous at osu1 dot php dot net | Date: | Fri, 14 May 2010 12:02:51 +0000 |
| Subject: | note 97900 added to function.array-flip | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169239@lists.php.net to get a copy of this message | ||
Further to:
benles -> crescentfreshpot -> mikeb
To flip an array without losing duplicate values and get a consistent data structure, with or with
out duplicates.
<?php
function array_invert($arr)
{
$flipped = array();
foreach( $arr as $key => $val ) {
$flipped[$val][] = $key;
}
return $flipped;
}
$a = array(
'orange' => 'fruit',
'milk' => 'dairy',
'apple' => 'fruit',
'banana' => 'fruit'
);
print_r(array_invert($a));
/*
Array
(
[fruit] => Array
(
[0] => orange
[1] => apple
[2] => banana
)
[dairy] => Array
(
[0] => milk
)
)
*/
?>
----
Server IP: 69.147.83.197
Probable Submitter: 86.4.216.43
----
Manual Page -- http://www.php.net/manual/en/function.array-flip.php
Edit -- https://master.php.net/note/edit/97900
Del: integrated -- https://master.php.net/note/delete/97900/integrated
Del: useless -- https://master.php.net/note/delete/97900/useless
Del: bad code -- https://master.php.net/note/delete/97900/bad+code
Del: spam -- https://master.php.net/note/delete/97900/spam
Del: non-english -- https://master.php.net/note/delete/97900/non-english
Del: in docs -- https://master.php.net/note/delete/97900/in+docs
Del: other reasons-- https://master.php.net/note/delete/97900
Reject -- https://master.php.net/note/reject/97900
Search -- https://master.php.net/manage/user-notes.php