26th June 2008 - 3 minutes read time
The array_flip() function in PHP is used to swap the values of an array with the keys. Take the following array.
$array = array('key1'=>'value1', 'key2'=>'value2');
To exchange all the values with the keys we pass it through the array_flip() function.
$array = array_flip($array);
echo '<pre>'.print_r($array, true).'</pre>';
This prints out the following:
Array
(
[value1] => key1
[value2] => key2
)
If any of the values are the same then the highest key is overwritten. The following array:
$array = array('a', 'a', 'a', 'b');
Will produce the following array when passed through array_flip().