PHP,数组-混序,steamapireturns不同序

PHP, array - mixed order, steam api returns different order

如何按数字 0 - 1+ 而不是按首字母排序?

error_reporting(E_ALL);
ini_set("display_errors", 1);

$players = array();             

$playerIds = [
    '76561197972192473',
    '76561198972352439',
    '76561198087304080',
    '76561198799985528',
    '76561198338485290'
];

$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=294CFDFSFCWE5EG5FE4&steamids=".implode(',', $playerIds);
$json_object= file_get_contents($url);
$json_decoded = json_decode($json_object);

foreach ($json_decoded->response->players as $key=>$player) {

    $players[] = [
        'name' => $player->personaname,
        'profileurl' => $player->profileurl,
        'avatarfull' => $player->avatarfull,
        'personastate' => $player->personastate
    ];
} 

usort($players, function($a, $b) {
    if ($a['name'] === $b['name']) {
        return 0;
    }

    return ($a['name'] < $b['name']) ? -1 : 1;
});

当我删除 usort 时,steam api 每次都会 return ID 以不同的顺序排列,所以你能帮我看看如何让它从 playerIds 中的 0 开始。

只需将 steamid 添加为索引,然后按键对其进行排序:

foreach ($json_decoded->response->players as $key=>$player) {
    $players[$player->steamid] = [
        'name' => $player->personaname,
        'profileurl' => $player->profileurl,
        'avatarfull' => $player->avatarfull,
        'personastate' => $player->personastate
    ];
}
ksort($players);

或者,您可以按您现在的方式进行,但将 steamid 添加到 $players 数组:

$players[] = [
    'id' => $player->steamid,
    'name' => $player->personaname,
    'profileurl' => $player->profileurl,
    'avatarfull' => $player->avatarfull,
    'personastate' => $player->personastate
];

然后按 id 排序:

array_multisort(array_column($players, 'id'), SORT_DESC, $players);

可能有一个参数要传递给 API,return 它们会按您想要的方式排序,不确定。