PHP: Exchange Array Keys and Values

To switch the position of keys and corresponding values in each element of an array, use the PHP function array_flip():

$a = array('1' => 'a', '2' => 'b', '3' => 'c');
$b = array_flip($a);
print_r($b);

And the output would be:

Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)
Scroll to Top