3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = <<<JSON { "mlb": { "james": { "avgTotalPoints": 80.675, "avgPointsDifferential": -0.8112500000000002, "rank": null }, "mary": { "avgTotalPoints": 114.9875, "avgPointsDifferential": 6.262500000000003, "rank": null }, "lucas": { "avgTotalPoints": 66.825, "avgPointsDifferential": -9.10625, "rank": null }, "timothy": { "avgTotalPoints": 69.2625, "avgPointsDifferential": -19.14125, "rank": null }, "samantha": { "avgTotalPoints": 91.625, "avgPointsDifferential": -36.85000000000001, "rank": null } }, "nhl": { "samantha": { "avgTotalPoints": 82.20000000000002, "avgPointsDifferential": 15.005714285714289, "rank": null }, "peter": { "avgTotalPoints": 77.65714285714284, "avgPointsDifferential": -13.31857142857143, "rank": null }, "rick": { "avgTotalPoints": 91.61428571428571, "avgPointsDifferential": 11.217142857142859, "rank": null }, "bubba": { "avgTotalPoints": 119.60000000000001, "avgPointsDifferential": 28.13333333333334, "rank": null }, "james": { "avgTotalPoints": 90.18571428571428, "avgPointsDifferential": 10.915714285714285, "rank": null } } } JSON; function sortHighestToLowest(array $arr): array { return array_map(static function (array $entries): array { uasort($entries, static function (array $entry1, array $entry2): int { return $entry2['avgTotalPoints'] <=> $entry1['avgTotalPoints']; }); $rank = 0; foreach ($entries as &$entry) { $entry['rank'] = ++$rank; } return $entries; }, $arr); } $arr = json_decode($json, true, 512, JSON_THROW_ON_ERROR); print_r(sortHighestToLowest($arr));
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
Array ( [mlb] => Array ( [mary] => Array ( [avgTotalPoints] => 114.9875 [avgPointsDifferential] => 6.2625 [rank] => 1 ) [samantha] => Array ( [avgTotalPoints] => 91.625 [avgPointsDifferential] => -36.85 [rank] => 2 ) [james] => Array ( [avgTotalPoints] => 80.675 [avgPointsDifferential] => -0.81125 [rank] => 3 ) [timothy] => Array ( [avgTotalPoints] => 69.2625 [avgPointsDifferential] => -19.14125 [rank] => 4 ) [lucas] => Array ( [avgTotalPoints] => 66.825 [avgPointsDifferential] => -9.10625 [rank] => 5 ) ) [nhl] => Array ( [bubba] => Array ( [avgTotalPoints] => 119.6 [avgPointsDifferential] => 28.133333333333 [rank] => 1 ) [rick] => Array ( [avgTotalPoints] => 91.614285714286 [avgPointsDifferential] => 11.217142857143 [rank] => 2 ) [james] => Array ( [avgTotalPoints] => 90.185714285714 [avgPointsDifferential] => 10.915714285714 [rank] => 3 ) [samantha] => Array ( [avgTotalPoints] => 82.2 [avgPointsDifferential] => 15.005714285714 [rank] => 4 ) [peter] => Array ( [avgTotalPoints] => 77.657142857143 [avgPointsDifferential] => -13.318571428571 [rank] => 5 ) ) )
Output for 7.2.0 - 7.2.33
Warning: Use of undefined constant JSON_THROW_ON_ERROR - assumed 'JSON_THROW_ON_ERROR' (this will throw an Error in a future version of PHP) in /in/YXP3o on line 78 Warning: json_decode() expects parameter 4 to be integer, string given in /in/YXP3o on line 78 Fatal error: Uncaught TypeError: Argument 1 passed to sortHighestToLowest() must be of the type array, null given, called in /in/YXP3o on line 79 and defined in /in/YXP3o:62 Stack trace: #0 /in/YXP3o(79): sortHighestToLowest(NULL) #1 {main} thrown in /in/YXP3o on line 62
Process exited with code 255.

preferences:
99.71 ms | 410 KiB | 5 Q