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?
The array_replace() function replaces the values of the first array with the values from following arrays. Tip: You can assign one array to the function, or as many as you like. If a key from array1 exists in array2, values from array1 will be replaced by the values from array2.
The array_flip() function flips/exchanges all keys with their associated values in an array.
Arrays in javascript are typically used only with numeric, auto incremented keys, but javascript objects can hold named key value pairs, functions and even other objects as well. Simple Array eg.
Use array_flip()
. That will do to swap keys with values. However, your array is OK the way it is. That is, you don't need to swap them, because then your array will become:
array(
'a' => 0,
'b' => 1,
'c' => 2
);
not
array(
'a',
'b',
'c'
);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With