Move an array element to a new index

1
2
3
4
5
6
7
8
$array = [0=>'a', 1=>'c', 2=>'d', 3=>'b', 4=>'e'];
 
function moveElement(&$array, $a, $b) {
    $out = array_splice($array, $a, 1);
    array_splice($array, $b, 0, $out);
}
 
moveElement($array, 3, 1);

result:

1
[0=>'a', 1=>'b', 2=>'c', 3=>'d', 4=>'e'];