从嵌套数组中获取值

时间:2015-12-09 06:22:09

标签: php arrays

我有这个数组:

array(4) { [0]=> array(4) { ["PlayerID"]=> int(0) ["Nickname"]=> string(15) "Peter" ["Score"]=> int(280) ["Ping"]=> int(322) } [1]=> array(4) { ["PlayerID"]=> int(1) ["Nickname"]=> string(4) "John" ["Score"]=> int(13265) ["Ping"]=> int(322) } [2]=> array(4) { ["PlayerID"]=> int(2) ["Nickname"]=> string(5) "Taylor" ["Score"]=> int(364) ["Ping"]=> int(281) } [3]=> array(4) { ["PlayerID"]=> int(7) ["Nickname"]=> string(7) "Mike" ["Score"]=> int(4656) ["Ping"]=> int(321) } }  

我使用了这个foreach循环:

$API = array(); 
foreach($API_GET as $ID => $Info)
 {
    $Player[$ID] = $Info;
    foreach($Info as $key => $value)
   { 
        $API[$key] = $value; 

    }
  }

并用它来回应彼得的绰号,但它似乎不起作用:

echo $Player['0']->$API[Nickname];

1 个答案:

答案 0 :(得分:5)

如果你这样做

var_dump($Player['0']);

你会得到

array(4) {
  ["PlayerID"]=>
  int(0)
  ["Nickname"]=>
  string(5) "Peter"
  ["Score"]=>
  int(280)
  ["Ping"]=>
  int(322)
}

所以要回应彼得的昵称,你将不得不这样做

echo $Player['0']['Nickname'];