Skip to content

Instantly share code, notes, and snippets.

@charleslouis
Created March 20, 2013 14:19
Show Gist options
  • Save charleslouis/5205022 to your computer and use it in GitHub Desktop.
Save charleslouis/5205022 to your computer and use it in GitHub Desktop.
enable swapping position of a key=>value pair backward. ie, if applied on "swap", [0]=>bar, [1]=>foo, [2]=>swap would become [0]=>bar, [1]=>swap, [2]=>foo
function array_swap_back($arr,$elem){
$ndx = array_search($elem,$arr);
$b4 = array_slice($arr,0,$ndx);
$mid = array_reverse(array_slice($arr,$ndx,2));
$after = array_slice($arr,$ndx + 2);
return array_merge($b4,$mid,$after);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment