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));

preferences:
22.45 ms | 407 KiB | 5 Q