Categories
PHP Tips & Tutorials

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
)

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.