PHP » Arrays » array_flip()

Syntax:
array array_flip(array array)
array
Array to be flipped.

Makes keys into elements and element into keys.

The array_flip() function changes an array so that the keys become the elements and the elements become the keys. A new array with the elements flipped is returned. If there are multiple occurrences of the same value in the original array, later occurrences will replace earlier ones as they become keys.

Examples

Code:
<pre>
<?php

$array1 = array(
   "Paris" => "France",
   "Rome" => "Italy",
   "New York" => "USA",
   "Los Angeles" => "USA");

$array1 = array_flip($array1);
print_r($array1);

?>
</pre>
Output:
Array
(
   [France] => Paris
   [Italy] => Rome
   [USA] => Los Angeles
)
Explanation:

Note that since there were two "USA" values and keys have to be unique, the "Los Angeles" entry overwrites the "New York" one.

See Also: