3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ [ "id" => "1", "macros" => [ "calorie" => 487.98, "fat" => 48.79, "carb" => 7.35, "protein" => 8.06 ], ], [ "id" => "3", "macros" => [ "calorie" => 472.07, "fat" => 38.4, "carb" => 9.46, "protein" => 18.25 ], ], [ "id" => "9", "macros" => [ "calorie" => 445.37, "fat" => 41.15, "carb" => 9.25, "protein" => 10.13 ], ], [ "id" => "18", "macros" => [ "calorie" => 457.73, "fat" => 41.92, "carb" => 5.61, "protein" => 10.94 ], ], [ "id" => "19", "macros" => [ "calorie" => 455.22, "fat" => 42.08, "carb" => 5.25, "protein" => 15.81 ] ], [ "id" => "200", "macros" => [ "calorie" => 443.21, "fat" => 35.95, "carb" => 3.86, "protein" => 24.3 ], ] ]; $optimal = [ "calorie" => 447.53, "fat" => 33.22, "carb" => 3.75, "protein" => 25.93 ]; $minMax = [ "calorie" => [ "minimum" => 406.84, "maximum" => 488.21 ], "fat" => [ "minimum" => 28.7, "maximum" => 37.74 ], "carb" => [ "minimum" => 0, "maximum" => 7.5 ], "protein" => [ "minimum" => 22.22, "maximum" => 29.63 ] ]; function sorter(array $array, $optimal, $minMax){ usort($array, function ($a, $b) use ($optimal, $minMax) { // check closeness of protein to optimal $apo = abs($a['macros']['protein'] - $optimal['protein']); $bpo = abs($b['macros']['protein'] - $optimal['protein']); // if not equally close, return the closest if ($apo != $bpo) return $apo <=> $bpo; // same protein difference to optimal, return the one with least carbs return $a['macros']['carb'] <=> $b['macros']['carb']; }); return $array; } print_r(sorter($array, $optimal, $minMax));

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.60.0100.01016.75
8.3.50.0140.00716.59
8.3.40.0100.00721.90
8.3.30.0120.00318.84
8.3.20.0040.00424.18
8.3.10.0040.00424.66
8.3.00.0050.00326.16
8.2.180.0000.01425.92
8.2.170.0130.00619.01
8.2.160.0130.00322.96
8.2.150.0050.00325.66
8.2.140.0000.00724.66
8.2.130.0000.00726.16
8.2.120.0040.00426.35
8.2.110.0040.00422.12
8.2.100.0060.00619.45
8.2.90.0040.00417.84
8.2.80.0040.00419.21
8.2.70.0030.00617.63
8.2.60.0060.00317.75
8.2.50.0040.00418.05
8.2.40.0050.00220.60
8.2.30.0030.00618.05
8.2.20.0040.00418.13
8.2.10.0080.00019.31
8.2.00.0000.00719.37
8.1.280.0180.00325.92
8.1.270.0000.00823.99
8.1.260.0080.00028.09
8.1.250.0030.00628.09
8.1.240.0060.00323.97
8.1.230.0070.00422.64
8.1.220.0060.00317.79
8.1.210.0040.00418.78
8.1.200.0030.00617.36
8.1.190.0000.00817.25
8.1.180.0000.00818.10
8.1.170.0060.00318.61
8.1.160.0020.00518.94
8.1.150.0000.00718.80
8.1.140.0070.00018.80
8.1.130.0000.00720.15
8.1.120.0000.00717.37
8.1.110.0040.00417.44
8.1.100.0000.00717.35
8.1.90.0040.00417.39
8.1.80.0050.00317.51
8.1.70.0040.00417.43
8.1.60.0080.00017.59
8.1.50.0030.00717.41
8.1.40.0080.00017.54
8.1.30.0060.00317.70
8.1.20.0050.00317.61
8.1.10.0000.00817.43
8.1.00.0080.00017.56
8.0.300.0050.00221.96
8.0.290.0070.00016.75
8.0.280.0000.00818.38
8.0.270.0050.00217.95
8.0.260.0000.00720.11
8.0.250.0070.00017.00
8.0.240.0030.00516.96
8.0.230.0000.00716.93
8.0.220.0040.00416.88
8.0.210.0000.00716.79
8.0.200.0000.00616.96
8.0.190.0040.00416.82
8.0.180.0000.00716.90
8.0.170.0060.00316.93
8.0.160.0040.00416.92
8.0.150.0030.00316.78
8.0.140.0040.00416.74
8.0.130.0030.00313.36
8.0.120.0000.00816.84
8.0.110.0040.00416.76
8.0.100.0030.00516.84
8.0.90.0040.00416.73
8.0.80.0110.00516.87
8.0.70.0060.00316.84
8.0.60.0040.00416.72
8.0.50.0000.00716.76
8.0.30.0060.01116.95
8.0.20.0080.01317.21
8.0.10.0000.00716.90
8.0.00.0060.01216.87
7.4.330.0000.00415.55
7.4.320.0000.00616.54
7.4.300.0030.00316.57
7.4.290.0030.00316.40
7.4.280.0040.00416.56
7.4.270.0030.00516.55
7.4.260.0050.00013.22
7.4.250.0000.00816.47
7.4.240.0030.00316.59
7.4.230.0040.00416.60
7.4.220.0050.00216.35
7.4.210.0060.00916.71
7.4.200.0040.00416.56
7.4.130.0080.01016.54
7.4.120.0190.00016.38
7.4.110.0050.01316.46
7.4.100.0080.00916.49
7.4.90.0100.00716.54
7.4.80.0080.01216.57
7.4.70.0130.00516.52
7.4.60.0120.00516.49
7.4.50.0100.00716.43
7.4.40.0100.00716.40
7.4.30.0110.00616.43
7.4.20.0090.00916.42
7.4.10.0130.00416.61
7.4.00.0120.00816.44
7.3.330.0030.00316.40
7.3.320.0000.00513.38
7.3.310.0030.00316.40
7.3.300.0000.00716.37
7.3.290.0080.00716.39
7.3.260.0100.01016.59
7.3.230.0110.00816.35
7.3.220.0080.00916.42
7.3.210.0120.00716.50
7.3.200.0140.00816.48
7.3.190.0050.01416.31
7.3.180.0110.00816.32
7.3.170.0080.00816.53
7.3.160.0100.00716.37
7.3.150.0120.00416.53
7.3.140.0030.01316.41
7.3.130.0060.01116.43
7.3.120.0070.01116.50
7.3.110.0040.01416.39
7.3.100.0100.00716.37
7.3.90.0080.00916.43
7.3.80.0080.01016.46
7.3.70.0080.00916.38
7.3.60.0130.00316.38
7.3.50.0140.00716.34
7.3.40.0100.01016.30
7.3.30.0110.00616.42
7.3.20.0180.00916.33
7.3.10.0100.00816.51
7.3.00.0080.01016.43
7.2.340.0050.01616.66
7.2.330.0070.01016.76
7.2.320.0090.01116.66
7.2.310.0080.01016.80
7.2.300.0090.00916.72
7.2.290.0110.00916.74
7.2.280.0110.01116.69
7.2.270.0100.00816.81
7.2.260.0090.00816.74
7.2.250.0110.00716.61
7.2.240.0060.01216.76
7.2.230.0100.01116.55
7.2.220.0090.00816.63
7.2.210.0060.01116.77
7.2.200.0090.00916.72
7.2.190.0110.00916.61
7.2.180.0110.01016.82
7.2.170.0060.01116.67
7.2.160.0040.01416.66
7.2.150.0090.00916.70
7.2.140.0090.00916.74
7.2.130.0110.00616.68
7.2.120.0190.00916.65
7.2.110.0070.01116.69
7.2.100.0100.01116.64
7.2.90.0090.01016.68
7.2.80.0090.00916.63
7.2.70.0110.00816.70
7.2.60.0090.01116.74
7.2.50.0070.01216.67
7.2.40.0100.00816.76
7.2.30.0080.01016.66
7.2.20.0130.00616.71
7.2.10.0140.00916.72
7.2.00.0090.01016.54

preferences:
69.09 ms | 400 KiB | 5 Q