php数组目标值按名称而不是数字

php数组目标值按名称而不是数字,php,arrays,json,api,object,Php,Arrays,Json,Api,Object,我只能以数字作为目标值,但api并不总是以相同的顺序返回它。因此,我希望能够以名称为目标(例如:total_kills),然后获取值(85990) 不要担心API键,它会被重置。您可以将数组转换为更方便使用的格式,例如,您可以将名称设置为新数组中的键,如下所示: print_r($stat_decoded['playerstats']['stats']); Array ( [ 0 ] => Array ( [ name ] =&g

我只能以数字作为目标值,但api并不总是以相同的顺序返回它。因此,我希望能够以名称为目标(例如:total_kills),然后获取值(85990)


不要担心API键,它会被重置。

您可以将数组转换为更方便使用的格式,例如,您可以将名称设置为新数组中的键,如下所示:

print_r($stat_decoded['playerstats']['stats']);



Array (
   [
      0
   ]   => Array (   [
      name
   ]   => total_kills   [
      value
   ]   => 85990 )   [
      1
   ]   => Array (   [
      name
   ]   => total_deaths   [
      value
   ]   => 88675 )   [
      2
   ]   => Array (   [
      name
   ]   => total_time_played   [
      value
   ]   => 7051848 )   [
      3
   ]   => Array (   [
      name
   ]   => total_planted_bombs   [
      value
   ]   => 2131 )   [
      4
   ]   => Array (   [
      name
   ]   => total_defused_bombs   [
      value
   ]   => 1234 )   [
      5
   ]   => Array (   [
      name
   ]   => total_wins   [
      value
   ]   => 47204 )

从这里,您可以通过接近其键中的值来获得值:
$assoc\u arr['total\u kills']

虽然您已经接受了答案,但更简单的版本是使用
array\u column()

$stat_url = 'http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=ECFB8FBC534B61C454899F4D7E99BB71&steamid=76561198129638121';
$stat_json = file_get_contents($stat_url);
$stat_decoded = json_decode($stat_json, true);

$assoc_arr = array_reduce($stat_decoded, function ($result, $item) {
    $result[$item['name']] = $item['value'];
    return $result;
}, []);
$stat_url = 'http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=ECFB8FBC534B61C454899F4D7E99BB71&steamid=76561198129638121';
$stat_json = file_get_contents($stat_url);
$stat_decoded = json_decode($stat_json, true);

$assoc_arr = array_reduce($stat_decoded, function ($result, $item) {
    $result[$item['name']] = $item['value'];
    return $result;
}, []);
$assoc_arr = array_column($stat_decoded['playerstats']['stats'], "value", "name");