public static function moveArrayKeyToPos($key, $pos, $arr): array
    {
        if (!isset($arr[$key])) {
            return $arr;
        }

        $tempValue = $arr[$key];
        unset($arr[$key]);

        if ($pos === 0) {
            return [$key => $tempValue] + $arr;
        }

        if ($pos > \count($arr)) {
            return $arr + [$key => $tempValue];
        }

        $new_arr = [];
        $i = 1;

        foreach ($arr as $arr_key => $arr_value) {
            if ($i === $pos) {
                $new_arr[$key] = $tempValue;
            }

            $new_arr[$arr_key] = $arr_value;
            ++$i;
        }

        return $new_arr;
    }
public static function moveArrayKeyToPos($key, $pos, $arr): array
    {
        if (!isset($arr[$key])) {
            return $arr;
        }

        $tempValue = $arr[$key];
        unset($arr[$key]);

        if ($pos === 0) {
            return [$key => $tempValue] + $arr;
        }

        if ($pos > \count($arr)) {
            return $arr + [$key => $tempValue];
        }

        $new_arr = [];
        $i = 1;

        foreach ($arr as $arr_key => $arr_value) {
            if ($i === $pos) {
                $new_arr[$key] = $tempValue;
            }

            $new_arr[$arr_key] = $arr_value;
            ++$i;
        }

        return $new_arr;
    }
public static function moveArrayKeyToPos ( $ key , $ pos , $ arr ) : array { if ( ! isset ( $ arr [ $ key ] ) ) { return $ arr ; } $ tempValue = $ arr [ $ key ] ; unset ( $ arr [ $ key ] ) ; if ( $ pos === 0 ) { return [ $ key => $ tempValue ] + $ arr ; } if ( $ pos > \ count ( $ arr ) ) { return $ arr + [ $ key => $ tempValue ] ; } $ new_arr = [ ] ; $ i = 1 ; foreach ( $ arr as $ arr_key => $ arr_value ) { if ( $ i === $ pos ) { $ new_arr [ $ key ] = $ tempValue ; } $ new_arr [ $ arr_key ] = $ arr_value ; ++ $ i ; } return $ new_arr ; }
Moves a named key in an associative array to a given position

@param string $key
@param int $pos
@param array $arr

@return array
Moves a named key in an associative array to a given position