3v4l.org

run code in 300+ PHP versions simultaneously
<?php function orderBeforeArray($name, $destination, $list) { // reindex the array so there are no gaps $list = array_values($list); $oldpos = array_search($name, $list); if (false === $oldpos) { throw new ItemNotFoundException("$name is not a child of this node"); } if ($destination == null) { // null means move to end $list = array_filter($list, function ($index) use ($oldpos) { return $index !== $oldpos; }, ARRAY_FILTER_USE_KEY); $list[] = $name; } else { // insert before element $destination $newpos = array_search($destination, $list); if ($newpos === false) { throw new ItemNotFoundException("$destination is not a child of this node"); } if ($oldpos < $newpos) { // we first unset, the position will change by one $newpos--; } unset($list[$oldpos]); array_splice($list, $newpos, 0, $name); } return $list; } $old = ['one', 'two', 'three', 'four']; $new = ['one', 'three', 'two']; $reorders = []; //check for deleted items $newIndex = array_flip($new); foreach ($old as $key => $value) { if (!isset($newIndex[$value])) { unset($old[$key]); } } // reindex the arrays to avoid holes in the indexes $old = array_values($old); $new = array_values($new); $len = count($new) - 1; $oldIndex = array_flip($old); //go backwards on the new node order and arrange them this way for ($i = $len; $i >= 0; $i--) { //get the name of the child node $current = $new[$i]; //check if it's not the last node if (isset($new[$i + 1])) { // get the name of the next node $next = $new[$i + 1]; //if in the old order $c and next are not neighbors already, do the reorder command if ($oldIndex[$current] + 1 != $oldIndex[$next]) { $reorders[$current] = $next; $old = orderBeforeArray($current, $next, $old); $oldIndex = array_flip($old); } } else { //check if it's not already at the end of the nodes if ($oldIndex[$current] != $len) { $reorders[$current] = null; $old = orderBeforeArray($current, null, $old); $oldIndex = array_flip($old); } } } var_dump($reorders);

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.3.130.0030.00516.60
8.3.120.0040.00419.14
8.3.110.0100.01020.94
8.3.100.0060.00624.06
8.3.90.0150.00018.19
8.3.80.0000.00918.55
8.3.70.0110.00418.45
8.3.60.0040.01416.88
8.3.50.0140.00817.52
8.3.40.0090.00918.65
8.3.30.0090.00918.67
8.3.20.0070.00020.34
8.3.10.0030.00622.10
8.3.00.0150.00323.66
8.2.250.0000.01116.50
8.2.240.0030.00618.76
8.2.230.0000.00822.58
8.2.220.0060.00337.54
8.2.210.0060.00326.77
8.2.200.0090.00018.29
8.2.190.0040.01116.58
8.2.180.0130.00916.47
8.2.170.0040.01222.96
8.2.160.0140.00020.52
8.2.150.0040.00424.18
8.2.140.0030.00524.66
8.2.130.0050.00320.53
8.2.120.0040.00426.35
8.2.110.0090.00021.02
8.2.100.0060.00617.78
8.2.90.0000.00820.39
8.2.80.0030.00520.36
8.2.70.0080.00017.63
8.2.60.0090.00019.78
8.2.50.0040.00418.10
8.2.40.0040.00422.38
8.2.30.0070.00019.36
8.2.20.0070.00018.13
8.2.10.0000.00818.07
8.2.00.0040.00418.18
8.1.300.0050.00518.41
8.1.290.0110.00018.88
8.1.280.0160.00325.92
8.1.270.0050.00323.99
8.1.260.0040.00426.35
8.1.250.0090.00028.09
8.1.240.0030.00622.14
8.1.230.0040.00822.75
8.1.220.0040.00417.74
8.1.210.0000.00818.84
8.1.200.0060.00317.38
8.1.190.0050.00317.35
8.1.180.0000.00818.10
8.1.170.0000.00918.57
8.1.160.0050.00218.82
8.1.150.0050.00320.20
8.1.140.0040.00417.40
8.1.130.0000.00718.94
8.1.120.0070.00417.48
8.1.110.0000.00917.38
8.1.100.0000.00817.35
8.1.90.0000.00817.42
8.1.80.0050.00217.44
8.1.70.0030.00717.43
8.1.60.0080.00017.61
8.1.50.0040.00417.54
8.1.40.0040.00417.44
8.1.30.0000.00817.63
8.1.20.0020.00517.53
8.1.10.0000.00917.52
8.1.00.0050.00317.45
8.0.300.0050.00319.96
8.0.290.0030.00716.63
8.0.280.0000.00718.38
8.0.270.0040.00417.27
8.0.260.0000.00718.40
8.0.250.0030.00317.01
8.0.240.0070.00016.94
8.0.230.0030.00517.00
8.0.220.0000.00716.86
8.0.210.0000.00716.89
8.0.200.0030.00317.03
8.0.190.0060.00316.88
8.0.180.0070.00016.96
8.0.170.0040.00416.83
8.0.160.0050.00216.97
8.0.150.0040.00416.68
8.0.140.0000.00816.86
8.0.130.0000.00613.34
8.0.120.0050.00216.83
8.0.110.0030.00516.86
8.0.100.0040.00416.93
8.0.90.0040.00416.93
8.0.80.0060.01616.89
8.0.70.0000.00716.74
8.0.60.0040.00416.84
8.0.50.0040.00416.78
8.0.30.0080.00917.02
8.0.20.0080.01317.40
8.0.10.0000.00717.00
8.0.00.0040.01316.62
7.4.330.0030.00315.55
7.4.320.0030.00316.52
7.4.300.0040.00416.62
7.4.290.0030.00716.52
7.4.280.0040.00416.61
7.4.270.0030.00316.51
7.4.260.0070.00016.50
7.4.250.0040.00416.47
7.4.240.0050.00216.54
7.4.230.0000.00716.34
7.4.220.0040.00416.51
7.4.210.0080.01116.61
7.4.200.0000.00716.43
7.4.160.0100.00616.35
7.4.150.0110.00717.40
7.4.140.0060.01017.86
7.4.130.0120.00616.50
7.4.120.0100.00816.57
7.4.110.0090.01216.43
7.4.100.0060.00916.57
7.4.90.0040.01316.55
7.4.80.0080.00819.39
7.4.70.0150.00416.39
7.4.60.0110.00416.48
7.4.50.0070.00316.70
7.4.40.0110.00616.60
7.4.30.0030.01316.56
7.4.00.0090.00715.00
7.3.330.0030.00313.32
7.3.320.0030.00313.25
7.3.310.0070.00016.42
7.3.300.0000.00716.25
7.3.290.0040.00416.39
7.3.280.0090.00816.36
7.3.270.0060.01117.40
7.3.260.0100.00916.59
7.3.250.0090.00716.35
7.3.240.0120.00616.46
7.3.230.0070.01016.69
7.3.210.0080.00816.40
7.3.200.0100.00716.58
7.3.190.0100.00616.50
7.3.180.0130.01016.32
7.3.170.0040.01116.65
7.3.160.0060.01216.55
7.3.120.0070.00714.94
7.3.110.0030.01414.74
7.3.100.0050.00715.00
7.3.90.0050.00714.73
7.3.80.0030.00814.90
7.3.70.0070.00814.79
7.3.60.0050.01214.84
7.3.50.0040.00514.80
7.3.40.0070.00514.79
7.3.30.0050.01014.78
7.3.20.0040.01016.77
7.3.10.0130.01116.00
7.3.00.0140.00816.04
7.2.330.0100.00716.69
7.2.320.0030.01516.78
7.2.310.0090.01316.69
7.2.300.0060.01016.85
7.2.290.0070.01016.55
7.2.250.0080.00815.25
7.2.240.0030.01515.12
7.2.230.0050.00915.11
7.2.220.0060.00415.20
7.2.210.0000.01215.07
7.2.200.0020.01415.05
7.2.190.0050.00815.11
7.2.180.0040.00915.07
7.2.170.0090.00715.13
7.2.130.0350.00314.98
7.2.120.0350.00315.04
7.2.110.0410.00615.07
7.2.100.0250.00414.68
7.2.90.0290.00314.90
7.2.80.0140.01015.82
7.2.70.0380.00914.88
7.2.60.0240.00615.92
7.2.50.1010.00014.54
7.2.40.0390.00814.65
7.2.30.0360.01014.77
7.2.20.0380.00014.94
7.2.10.0410.01014.93
7.2.00.0290.00715.12
7.1.330.0060.01015.96
7.1.320.0050.01015.71
7.1.310.0050.00815.65
7.1.300.0040.01115.80
7.1.290.0080.00615.81
7.1.280.0030.00815.84
7.1.270.0050.00815.67
7.1.260.0050.01115.83
7.1.250.0290.00313.81
7.1.240.0290.00714.23
7.1.230.0380.00313.91
7.1.220.0200.00714.02
7.1.210.0310.00014.00
7.1.200.0210.00814.82
7.1.190.0370.00914.05
7.1.180.0270.00714.23
7.1.170.0280.00414.12
7.1.160.0300.00313.98
7.1.150.0240.01214.07
7.1.140.0330.00013.86
7.1.130.0320.00314.03
7.1.120.0390.00314.10
7.1.110.0470.00614.16
7.1.100.0340.01315.20
7.1.90.5260.00715.05
7.1.80.4280.01215.13
7.1.70.3170.00714.56
7.1.60.3600.01023.48
7.1.50.3360.01723.39
7.1.40.3190.01223.24
7.1.30.3920.00323.22
7.1.20.3950.01123.26
7.1.10.3560.00814.49
7.1.00.3620.00514.40
7.0.330.0330.00713.48
7.0.320.0330.01313.69
7.0.310.0480.00913.38
7.0.300.0410.01413.64
7.0.290.0480.00713.36
7.0.280.0530.00913.35
7.0.270.0230.00313.60
7.0.260.0140.01413.67
7.0.250.0460.01113.62
7.0.240.0250.01014.81
7.0.230.5210.01014.67
7.0.220.4010.00814.60
7.0.210.3520.00714.25
7.0.200.4130.00814.34
7.0.190.4010.00714.38
7.0.180.3030.00814.11
7.0.170.5040.00814.07
7.0.160.4420.00513.91
7.0.150.3790.00213.97
7.0.140.2610.01114.09
7.0.130.2790.01414.31
7.0.120.3150.01014.19
7.0.110.2900.00514.04
7.0.100.3150.00514.14
7.0.90.2780.00514.09
7.0.80.2240.00514.09
7.0.70.2250.00614.00
7.0.60.2450.01113.95
7.0.50.2900.00914.15
7.0.40.2450.00614.09
7.0.30.2720.01014.20
7.0.20.2620.00814.14
7.0.10.2630.00814.11
7.0.00.2780.00514.04
5.6.380.0070.01314.23

preferences:
27.73 ms | 403 KiB | 5 Q