PHP Array Associatif Element Move
function moveKeyBefore($arr, $find, $move) {
if (!isset($arr[$find], $arr[$move])) {
return $arr;
}
$elem = [$move=>$arr[$move]]; // cache the element to be moved
$start = array_splice($arr, 0, array_search($find, array_keys($arr)));
unset($start[$move]); // only important if $move is in $start
return $start + $elem + $arr;
}
$arr = ['foo1'=>'bar', 'foo2'=>'buzz', 'foo3'=>'bazz'];
var_export(moveKeyBefore($arr, 'foo3', 'foo1'));
cadot.eu