【问题标题】:PHP, array - mixed order, steam api returns different orderPHP,数组-混合顺序,steam api返回不同顺序
【发布时间】:2020-05-05 15:50:27
【问题描述】:

我如何按数字排序,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 每次都会以不同的顺序返回 ids,所以你能帮我如何让它从 playerIds 中的 0 开始。

【问题讨论】:

  • $player-&gt;playerid ???返回的id叫什么名字?

标签: php steam steam-web-api


【解决方案1】:

只需将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,它会按照你想要的方式返回它们,不确定。

【讨论】:

    猜你喜欢
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多