我目前正在寻找一种有效的方法来根据最高 -> 最低使用对数组/对象进行排序avgTotalPoints,然后根据字段中的排名放置数值rank。我查看了一些示例并能够相应地创建一个函数。然而,它现在正在发挥作用。我试图访问avgTotalPoints下面所示的排序函数,但我无法访问,因为数组是多深度的。关于如何实现这一目标有任何指导吗?
function sortHighestToLowest($arr)
{
if (isset($arr) && !empty($arr)) {
uasort($arr, function ($a, $b) {
return $a['avgTotalPoints'] <=> $b['avgTotalPoints'];
});
return array_reverse($arr);
}
return null;
}
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
}
}
}
鸿蒙传说