获取最高值max()的名称

时间:2015-08-01 22:50:48

标签: php

如何获取最高的名称变量?
示例代码:max($ 1,$ 2,$ 3)和$ 2值是“32”并且它是最高的,那么如何将$ 2保存到另一个var? 我的代码:

$stats = file_get_contents('http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=**********&steamid='.$id); //example id - 76561198019477694
$stats = json_decode($stats, true);

$fav_gun1 = $stats["playerstats"]["stats"]["9"]["value"];
$fav_gun2 = $stats["playerstats"]["stats"]["10"]["value"];
$fav_gun3 = $stats["playerstats"]["stats"]["11"]["value"];
$fav_gun4 = $stats["playerstats"]["stats"]["12"]["value"];
$fav_gun5 = $stats["playerstats"]["stats"]["13"]["value"];
$fav_gun6 = $stats["playerstats"]["stats"]["14"]["value"];
$fav_gun7 = $stats["playerstats"]["stats"]["15"]["value"];
$fav_gun8 = $stats["playerstats"]["stats"]["16"]["value"];
$fav_gun9 = $stats["playerstats"]["stats"]["17"]["value"];
$fav_gun10 = $stats["playerstats"]["stats"]["18"]["value"];
$fav_gun11 = $stats["playerstats"]["stats"]["19"]["value"];
$fav_gun12 = $stats["playerstats"]["stats"]["20"]["value"];
$fav_gun13 = $stats["playerstats"]["stats"]["21"]["value"];
$fav_gun14 = $stats["playerstats"]["stats"]["22"]["value"];
$fav_gun15 = $stats["playerstats"]["stats"]["23"]["value"];
$fav_gun16 = $stats["playerstats"]["stats"]["24"]["value"];

$fav_gun = max($fav_gun1, $fav_gun2, $fav_gun3, $fav_gun4, $fav_gun5,    $fav_gun6, $fav_gun7, $fav_gun8, $fav_gun9, $fav_gun10, $fav_gun11, $fav_gun12, $fav_gun13, $fav_gun14, $fav_gun15, $fav_gun16);

// $fav_gun returns number of kills with specified gun, i want to get name of this gun ($stats["playerstats"]["stats"]["xx"]["name"])

2 个答案:

答案 0 :(得分:1)

尝试

$fav_gun1 = (int) $stats["playerstats"]["stats"]["9"]["value"];
$fav_gun2 = (int) $stats["playerstats"]["stats"]["10"]["value"];
$fav_gun3 = (int) $stats["playerstats"]["stats"]["11"]["value"];
$fav_gun4 = (int) $stats["playerstats"]["stats"]["12"]["value"];
$fav_gun5 = (int) $stats["playerstats"]["stats"]["13"]["value"];
$fav_gun6 = (int) $stats["playerstats"]["stats"]["14"]["value"];
$fav_gun7 = (int) $stats["playerstats"]["stats"]["15"]["value"];
$fav_gun8 = (int) $stats["playerstats"]["stats"]["16"]["value"];
$fav_gun9 = (int) $stats["playerstats"]["stats"]["17"]["value"];
$fav_gun10 = (int) $stats["playerstats"]["stats"]["18"]["value"];
$fav_gun11 = (int) $stats["playerstats"]["stats"]["19"]["value"];
$fav_gun12 = (int) $stats["playerstats"]["stats"]["20"]["value"];
$fav_gun13 = (int) $stats["playerstats"]["stats"]["21"]["value"];
$fav_gun14 = (int) $stats["playerstats"]["stats"]["22"]["value"];
$fav_gun15 = (int) $stats["playerstats"]["stats"]["23"]["value"];
$fav_gun16 = (int) $stats["playerstats"]["stats"]["24"]["value"];

$fav_guns_tab = array($fav_gun1, $fav_gun2, $fav_gun3, $fav_gun4, $fav_gun5, $fav_gun6, $fav_gun7, $fav_gun8, $fav_gun9, $fav_gun10, $fav_gun11, $fav_gun12, $fav_gun13, $fav_gun14, $fav_gun15, $fav_gun16);

$fav_gun = array_keys($fav_guns_tab, max($fav_guns_tab));

答案 1 :(得分:1)

循环遍历数组,同时跟踪与最高值关联的名称:

$highest_name = false;
$highest_value = false;

foreach($stats["playerstats"]["stats"] as $stats) {
    if($highest_name === false || $stats['value'] > $highest_value) {
        $highest_name = $stats['name'];
        $highest_value = $stats['value'];
    }
}

print $highest_name;