note 93505 deleted from function.array-flip by danbrown

From: Date: Mon, 14 Sep 2009 02:00:26 +0000
Subject: note 93505 deleted from function.array-flip by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160526@lists.php.net to get a copy of this message
Note Submitter: Vincenzo (nemo at neminis dot org) ---- 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 ) )

« previous php.notes (#160526) next »