- var_dump: documentation ( source)
- json_decode: documentation ( source)
- usort: documentation ( source)
<?php
// Raw data
$json = <<<'JSON'
{
"player1": {
"money": 1000,
"kills": 6
},
"player2": {
"money": 3000,
"kills": 12
},
"player3": {
"money": 2000,
"kills": 2
}
}
JSON;
// Prepare
$sortBy = 'money';
$order = 'desc';
$dataset = json_decode($json, true);
// Sorting
usort($dataset, fn (array $playerA, array $playerB): int => ($playerA['money'] > $playerB['money'] ? -1 : 1) * ['desc' => 1, 'asc' => -1][$order] ?? -1);
// Print results
var_dump($dataset);