Forum Moderators: coopster
I need to swap 2 elements in an associative array (Both key and it's respective value need to swap places in the array) yet I can't find a way for PHP to allow me to do something like
$array[0] = $array[1]
since it is an associative array and numerical indexes dont exist.
How do I tackle this problem?
An example would be:
$array = array("banana"=>"yellow","apple"=>"red","kiwi"=>"green","blueberry"=>"blue");swapFunction($array,"apple","kiwi");
print_r($array);
//The above results in array("banana"=>"yellow","kiwi"=>"green","apple"=>"red","blueberry"=>"blue")
As you might notice from the example above the reason for such a swap if for pure sorting purposes (And as the array will be used to show a table in HTML it needs to be sorted according to what the user chooses).
Nutter, thanks for the example but the one you mention swaps the values in between 2 keys which is I dont want to do. I want the keys to hold their values, just swap places.