For example i have an array like this $array = ('a' => 2, 'b' => 1, 'c' => 4);
and i need to swap a
with c
to get this $array = ('c' => 4, 'b' => 1, 'a' => 2);
. Wht is the best way for doing this without creating new array? I know that it is possible with XOR, but i also need to save indexes.
Asked
Active
Viewed 2,003 times
0

Kin
- 4,466
- 13
- 54
- 106
-
What kind of logic does the swap follow? – kero Dec 06 '13 at 14:41
-
@kingkero keys represents some names in a particular order. – Kin Dec 06 '13 at 14:42
-
@Pranavc as i said before - i need to save key => value structure. – Kin Dec 06 '13 at 14:43
-
And you can't use [`uksort()`](http://www.php.net/manual/function.uksort.php) for this? Sorry, but we'll need more information to give you an appropriate answer – kero Dec 06 '13 at 14:46
-
@Pranavc OP doesn't want to change values only but keep the associative indices! – kero Dec 06 '13 at 14:46
-
I would think `array_splice` is your friend here. – deceze Dec 06 '13 at 14:47
-
@kingkero i need to swap just two values, not all array. I just know that the default order is `a,b,c` and i need to change to `c,b,a` saving it's values. – Kin Dec 06 '13 at 14:48
-
perhaps `list` may help you?.. like: `list ($array['a'],$array['c']) = array ($array['c'],$array['a']);` – briosheje Dec 06 '13 at 14:50
-
@Kirix then deceze's comment seems to be quite an eligable solution – kero Dec 06 '13 at 14:53
-
possible duplicate of [Switch two items in associative array](http://stackoverflow.com/questions/2448345/switch-two-items-in-associative-array) – towr Dec 06 '13 at 14:56
-
@briosheje nope, it just swaps values. And i need to swap keys with valuest – Kin Dec 06 '13 at 14:56
2 Answers
1
array_splice
would be perfect, but unfortunately it doesn't preserve the keys in the inserted arrays. So you'll have to resort to a little more manual slicing and dicing:
function swapOffsets(array $array, $offset1, $offset2) {
list($offset1, $offset2) = array(min($offset1, $offset2), max($offset1, $offset2));
return array_merge(
array_slice($array, 0, $offset1, true),
array_slice($array, $offset2, 1, true),
array_slice($array, $offset1 + 1, $offset2 - $offset1 - 1, true),
array_slice($array, $offset1, 1, true),
array_slice($array, $offset2 + 1, null, true)
);
}

deceze
- 510,633
- 85
- 743
- 889
0
If you want to purely swap the first and the last positions, here's one way to do it:
$first = array(key($array) => current($array)); // Get the first key/value pair
array_shift($array); // Remove it from your array
end($array);
$last = array(key($array) => current($array)); // Get the last key/value pair
array_pop($array); // Remove it from the array
$array = array_merge($last, $array, $first); // Put it back together
Gives you:
Array
(
[c] => 4
[b] => 1
[a] => 2
)
Working example: http://3v4l.org/r87qD
Update: And just for fun, you can squeeze that down quite a bit:
$first = array(key($array) => current($array));
$last = array_flip(array(end($array) => key($array)));
$array = array_merge($last, array_slice($array,1,count($array) - 2), $first);
Working example: http://3v4l.org/v6R7T
Update 2:
Oh heck yeah, we can totally do this in one line of code now:
$array = array_merge(array_flip(array(end($array) => key($array))), array_slice($array,1,count($array) - 2), array_flip(array(reset($array) => key($array))));
Working example: http://3v4l.org/QJB5T
That was fun, thanks for the challenge. =)

jszobody
- 28,495
- 6
- 61
- 72
-
-
-
I see. I should've read all the comments. It didn't occur to me that the order of the keys was important. My apologies. – Crashspeeder Dec 06 '13 at 16:55
-
@Crashspeeder No biggie. It is an odd question, I'm curious why the OP really even needs to do this. But fun nonetheless. – jszobody Dec 06 '13 at 16:56
-
note, id didmt. I didn't say that i need to swap first/last, i said i need to swap one with other. – Kin Dec 09 '13 at 06:45