- Output for 8.1.0
- array(3) { [0]=> array(2) { ["money"]=> int(3000) ["kills"]=> int(12) } [1]=> array(2) { ["money"]=> int(2000) ["kills"]=> int(2) } [2]=> array(2) { ["money"]=> int(1000) ["kills"]=> int(6) } }
<?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);