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 git.master, git.master_jit, rfc.property-hooks
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 ) ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
39.12 ms | 408 KiB | 9 Q