How to swap keys with values in array?

daGrevis picture daGrevis · Jul 27, 2011 · Viewed 20.8k times · Source

I have array like:

array(
  0 => 'a',
  1 => 'b',
  2 => 'c'
);

I need to convert it to:

array(
  'a',
  'b',
  'c'
);

What's the fastest way to swap keys with values?

Answer

Haim Evgi picture Haim Evgi · Jul 27, 2011

PHP has the array_flip function which exchanges all keys with their corresponding values, but you do not need it in your case because the arrays are the same.

array(
  'a',
  'b',
  'c'
);

This array has the keys 0, 1, and 2.