3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Reposition an array element by on its key. * * @param array $array The array being reordered. * @param string|int $key They key of the element you want to reposition. * @param int $order The position in the array you want to move the element to. (0 is first) * * @throws \Exception */ function repositionArrayElement(array &$array, $key, int $order): void { if(($a = array_search($key, array_keys($array))) === false){ throw new \Exception("The {$key} cannot be found in the given array."); } $p1 = array_splice($array, $a, 1); $p2 = array_splice($array, 0, $order); $array = array_merge($p2, $p1, $array); } $fruits = [ 'bananas'=>'12', 'apples'=>'23', 'tomatoes'=>'21', 'nuts'=>'22', 'foo'=>'a', 'bar'=>'b' ]; repositionArrayElement($fruits, "foo", 1); var_export($fruits); $colours = ["green", "blue", "red"]; repositionArrayElement($colours, 2, 0); var_export($colours);

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.4.10.0080.00819.41
8.3.140.0030.01218.89
8.3.130.0090.00016.79
8.3.120.0250.00518.42
8.3.110.0190.00718.72
8.3.100.0240.00416.56
8.3.90.0210.00723.27
8.3.80.0250.00320.80
8.3.70.0210.00820.52
8.3.60.0220.00719.93
8.3.50.0210.00523.05
8.3.40.0220.00620.89
8.3.30.0140.00820.84
8.3.20.0160.00522.69
8.3.10.0170.00422.81
8.3.00.0170.00423.42
8.2.260.0150.00317.30
8.2.250.0000.00818.30
8.2.240.0090.01016.77
8.2.230.0150.00819.50
8.2.220.0120.00920.35
8.2.210.0130.01223.27
8.2.200.0200.00820.75
8.2.190.0300.01019.84
8.2.180.0250.00823.07
8.2.170.0300.00521.01
8.2.160.0270.00322.38
8.2.150.0250.00423.27
8.2.140.0270.00822.91
8.2.130.0230.00523.42
8.2.120.0180.00621.60
8.2.110.0120.00721.72
8.2.100.0250.00720.64
8.2.90.0210.00921.54
8.2.80.0260.00620.63
8.2.70.0230.00720.62
8.2.60.0240.00420.63
8.2.50.0180.00920.66
8.2.40.0150.00719.99
8.2.30.0160.00619.96
8.2.20.0180.00619.73
8.2.10.0170.00620.11
8.2.00.0120.00619.68
8.1.300.0100.01216.83
8.1.290.0170.00724.63
8.1.280.0220.00322.95
8.1.270.0190.00522.65
8.1.260.0240.00623.46
8.1.250.0230.00824.01
8.1.240.0150.00921.22
8.1.230.0200.00922.00
8.1.220.0210.00720.51
8.1.210.0220.01120.82
8.1.200.0220.01120.32
8.1.190.0200.00420.20
8.1.180.0160.00420.58
8.1.170.0140.00620.05
8.1.160.0130.00419.88
8.1.150.0140.00619.77
8.1.140.0160.00420.18
8.1.130.0180.00219.57
8.1.120.0150.00419.62
8.1.110.0140.00519.61
8.1.100.0140.00519.64
8.1.90.0140.00719.64
8.1.80.0140.00619.58
8.1.70.0100.00718.99
8.1.60.0170.00619.77
8.1.50.0140.00719.72
8.1.40.0150.00819.28
8.1.30.0170.00518.99
8.1.20.0200.00819.44
8.1.10.0170.00519.08
8.1.00.0180.00518.98
8.0.300.0200.00521.04
8.0.290.0260.00420.06
8.0.280.0180.00519.44
8.0.270.0170.00419.35
8.0.260.0070.01119.18
8.0.250.0150.00419.14
8.0.240.0140.00719.18
8.0.230.0170.00419.10
8.0.220.0200.00519.18
8.0.210.0170.00719.03
8.0.200.0200.00419.13
8.0.190.0180.00719.22
8.0.180.0190.00519.15
8.0.170.0170.00618.76
8.0.160.0200.00518.75
8.0.150.0150.00818.74
8.0.140.0120.00818.47
8.0.130.0180.00317.97
8.0.120.0140.00518.46
8.0.110.0120.00618.45
8.0.100.0150.00518.46
8.0.90.0170.00418.46
8.0.80.0150.00818.25
8.0.70.0160.00518.41
8.0.60.0140.00618.43
8.0.50.0150.00718.52
8.0.30.0140.00818.48
8.0.20.0130.00818.39
8.0.10.0140.00618.54
8.0.00.0130.00618.57
7.4.330.0180.00418.77
7.4.320.0170.00518.94
7.4.300.0180.00318.96
7.4.290.0160.00318.87
7.4.280.0120.00518.56
7.4.270.0090.00918.29
7.4.260.0150.00317.79
7.4.250.0110.00618.27
7.4.240.0110.00618.23
7.4.230.0130.00418.26
7.4.220.0090.00618.03
7.4.210.0110.00617.88
7.4.200.0130.00318.25
7.4.190.0180.00418.57
7.4.180.0140.00918.55
7.4.160.0130.00818.60
7.4.150.0170.00318.51
7.4.140.0150.00518.49
7.4.130.0110.00618.03
7.4.120.0160.00618.27
7.4.110.0130.00418.24
7.4.100.0120.00618.22
7.4.90.0110.00618.28
7.4.80.0130.00718.15
7.4.70.0130.00418.24
7.4.60.0140.00618.06
7.4.50.0110.00518.11
7.4.40.0130.00418.10
7.4.30.0130.00618.20
7.4.20.0140.00418.20
7.4.10.0160.00518.15
7.4.00.0150.00418.23
7.3.330.0160.00418.08
7.3.320.0130.00517.59
7.3.310.0150.00418.09
7.3.300.0150.00418.06
7.3.290.0140.00517.71
7.3.280.0160.00518.00
7.3.270.0200.00618.36
7.3.260.0180.00418.14
7.3.250.0170.00618.31
7.3.240.0190.00318.37
7.3.230.0160.00418.12
7.3.220.0180.00618.38
7.3.210.0160.00618.09
7.3.200.0180.00818.13
7.3.190.0150.00918.07
7.3.180.0190.00418.12
7.3.170.0150.00718.04
7.3.160.0180.00618.06
7.3.150.0160.00518.06
7.3.140.0140.00718.13
7.3.130.0150.00518.10
7.3.120.0160.00418.06
7.3.110.0180.00318.03
7.3.100.0170.00418.00
7.3.90.0170.00518.20
7.3.80.0170.00618.05
7.3.70.0170.00718.10
7.3.60.0200.00318.15
7.3.50.0160.00618.13
7.3.40.0160.00718.13
7.3.30.0150.00718.13
7.3.20.0200.00418.91
7.3.10.0190.00518.87
7.3.00.0180.00718.89
7.2.340.0170.00518.40
7.2.330.0170.00618.14
7.2.320.0150.00618.18
7.2.310.0150.00518.15
7.2.300.0150.00518.14
7.2.290.0130.00818.14
7.2.280.0170.00618.12
7.2.270.0170.00618.06
7.2.260.0170.00618.12
7.2.250.0170.00518.20
7.2.240.0160.00718.17
7.2.230.0160.00618.14
7.2.220.0160.00818.09
7.2.210.0160.00718.11
7.2.200.0190.00518.23
7.2.190.0150.00518.24
7.2.180.0150.00718.17
7.2.170.0150.00518.25
7.2.160.0140.00918.32
7.2.150.0180.00819.06
7.2.140.0170.00719.02
7.2.130.0180.00619.08
7.2.120.0180.00619.07
7.2.110.0170.00618.99
7.2.100.0180.00719.05
7.2.90.0170.00618.98
7.2.80.0190.00419.03
7.2.70.0190.00519.05
7.2.60.0170.00519.02
7.2.50.0180.00419.00
7.2.40.0180.00619.03
7.2.30.0130.00919.08
7.2.20.0140.00719.05
7.2.10.0180.00419.06
7.2.00.0150.00519.33

preferences:
50.95 ms | 403 KiB | 5 Q