我正在创建一个顶级站点,我有一个关于回显多维数组的某个元素的问题。我使用名为GameQ的库来ping游戏服务器以获取所述服务器的统计信息。我无法打印我指定的数组中某些元素的结果。
我的代码:
<?php
// Include the main class file
require '../GameQ.php';
$gq = new GameQ();
$gq->addServer(array(
'id' => '0',
'type' => 'minecraft',
'host' => '192.210.238.76:25865',
));
$results = $gq->requestData(); // Returns an array of results
//Below line returns a error: Notice: Undefined offset: 0
echo "Online: " . $results[0]['numplayers'] . "/" . $results[0]['maxplayers'] . "<br />";
var_dump($results);
?>
Array Var转储:
array (size=1)
'192.210.238.76:25865' =>
array (size=17)
'hostname' => string 'A Whole Other Level' (length=19)
'gametype' => string 'SMP' (length=3)
'game_id' => string 'MINECRAFT' (length=9)
'version' => string '1.6.4' (length=5)
'plugins' => string 'CraftBukkit on Bukkit 1.6.4-R0.1-SNAPSHOT' (length=41)
'map' => string 'Build' (length=5)
'numplayers' => string '6' (length=1)
'maxplayers' => string '30' (length=2)
'hostport' => string '25865' (length=5)
'hostip' => string '192.210.238.76' (length=14)
'players' =>
array (size=6)
0 =>
array (size=1)
...
1 =>
array (size=1)
...
2 =>
array (size=1)
...
3 =>
array (size=1)
...
4 =>
array (size=1)
...
5 =>
array (size=1)
...
'gq_online' => boolean true
'gq_address' => string '192.210.238.76' (length=14)
'gq_port' => string '25865' (length=5)
'gq_protocol' => string 'gamespy3' (length=8)
'gq_type' => string 'minecraft' (length=9)
'gq_transport' => string 'udp' (length=3)
我已在Manual查看了数组的手册。我很感激任何人对这个主题的帮助。我知道我错过了什么。在注册提出这个问题之前,我先尝试搜索答案。我认为搜索结果会比等待某人帮我解答更快。感谢您的患者和您的理解。快乐的编码!
答案 0 :(得分:0)
数组中不存在键0,请尝试使用
$results['192.210.238.76:25865']['numplayers']
修改强>
由于这可能是变量,你可以获取数组的第一个元素而不用这种方式修改数组:
$firstValue = array_shift(array_values($results)); //the array which is the first value
$numplayers = $firstValue['numplayers'];
中找到的行