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));
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Array ( [0] => Array ( [id] => 200 [macros] => Array ( [calorie] => 443.21 [fat] => 35.95 [carb] => 3.86 [protein] => 24.3 ) ) [1] => Array ( [id] => 3 [macros] => Array ( [calorie] => 472.07 [fat] => 38.4 [carb] => 9.46 [protein] => 18.25 ) ) [2] => Array ( [id] => 19 [macros] => Array ( [calorie] => 455.22 [fat] => 42.08 [carb] => 5.25 [protein] => 15.81 ) ) [3] => Array ( [id] => 18 [macros] => Array ( [calorie] => 457.73 [fat] => 41.92 [carb] => 5.61 [protein] => 10.94 ) ) [4] => Array ( [id] => 9 [macros] => Array ( [calorie] => 445.37 [fat] => 41.15 [carb] => 9.25 [protein] => 10.13 ) ) [5] => Array ( [id] => 1 [macros] => Array ( [calorie] => 487.98 [fat] => 48.79 [carb] => 7.35 [protein] => 8.06 ) ) )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Array ( [0] => Array ( [id] => 200 [macros] => Array ( [calorie] => 443.21 [fat] => 35.95 [carb] => 3.86 [protein] => 24.3 ) ) [1] => Array ( [id] => 3 [macros] => Array ( [calorie] => 472.07 [fat] => 38.4 [carb] => 9.46 [protein] => 18.25 ) ) [2] => Array ( [id] => 19 [macros] => Array ( [calorie] => 455.22 [fat] => 42.08 [carb] => 5.25 [protein] => 15.81 ) ) [3] => Array ( [id] => 18 [macros] => Array ( [calorie] => 457.73 [fat] => 41.92 [carb] => 5.61 [protein] => 10.94 ) ) [4] => Array ( [id] => 9 [macros] => Array ( [calorie] => 445.37 [fat] => 41.15 [carb] => 9.25 [protein] => 10.13 ) ) [5] => Array ( [id] => 1 [macros] => Array ( [calorie] => 487.98 [fat] => 48.79 [carb] => 7.35 [protein] => 8.06 ) ) )

preferences:
158.54 ms | 405 KiB | 182 Q