我有一个玩家列表的多维数组,用于使命召唤4.当我尝试回应数组时,它会回复Array
30次,因为服务器中有30个当前玩家。
$ promodplist的Var_Dump(玩家列表)
array(27) {
[0]=> array(6) {
["frags"]=> string(1) "0"
["ping"]=> string(2) "26"
["nick"]=> string(10) "DIVINEBRAH"
["gq_name"]=> string(10) "DIVINEBRAH"
["gq_score"]=> string(1) "0"
["gq_ping"]=> string(2) "26"
}
[1]=> array(6) {
["frags"]=> string(1) "0"
["ping"]=> string(2) "35"
["nick"]=> string(7) "><> <><"
["gq_name"]=> string(7) "><> <><"
["gq_score"]=> string(1) "0"
["gq_ping"]=> string(2) "35"
}
[2]=> array(6) {
["frags"]=> string(1) "0"
["ping"]=> string(2) "42"
["nick"]=> string(10) "xXthe0neXx"
["gq_name"]=> string(10) "xXthe0neXx"
["gq_score"]=> string(1) "0"
["gq_ping"]=> string(2) "42"
}
$servers['promod'] = array('cod4', '67.202.102.224');
$servers['promod2'] = array('cod4', '67.202.102.224');
$gq = new GameQ();
$gq->addServers($servers);
$results = $gq->requestData();
function print_results($results) {
foreach ($results as $id => $data)
这就是我想用来列出现有球员的内容。
$promodplist = $data['promod']['players'];
foreach($promodplist as $k => $v)
我只是想在每个数组中回显nick
(昵称)。
答案 0 :(得分:8)
$promodplist = $data['promod']['players'];
foreach($promodplist as $k => $v)
print($v['nick']);
应该做你想做的事。 foreach
遍历数组中的键/值对,其中$k
是元素的键(在您的情况下是基于0的索引),$v
是值(数组)玩家数据,为你)。您可以使用其名称作为array accessor中的密钥来访问其余信息。
答案 1 :(得分:2)
这样的功能怎么样
function print_results($results) {
foreach ($results as $id){
echo ": Player Info :<br />";
foreach($id as $key => $val){
// add this line to only print out the "nick" field
if ($key == "nick")
echo "Field: " . $key . " - Value: " . $val . "<br />";
}
}
}
此外,格式化阵列有助于我们了解您正在使用的内容;一条长线比破译更难解读:
array(27) {
[0]=> array(6) {
["frags"]=> string(1) "0"
["ping"]=> string(2) "26"
["nick"]=> string(10) "DIVINEBRAH"
["gq_name"]=> string(10) "DIVINEBRAH"
["gq_score"]=> string(1) "0"
["gq_ping"]=> string(2) "26"
}
[1]=> array(6) {
["frags"]=> string(1) "0"
["ping"]=> string(2) "35"
["nick"]=> string(7) "><> <><"
["gq_name"]=> string(7) "><> <><"
["gq_score"]=> string(1) "0"
["gq_ping"]=> string(2) "35"
}
[2]=> array(6) {
["frags"]=> string(1) "0"
["ping"]=> string(2) "42"
["nick"]=> string(10) "xXthe0neXx"
["gq_name"]=> string(10) "xXthe0neXx"
["gq_score"]=> string(1) "0"
["gq_ping"]=> string(2) "42"
}
[3]=> array(6) {
["frags"]=> string(1) "0"
["ping"]=> string(2) "50"
["nick"]=> string(5) "GenKY"
["gq_name"]=> string(5) "GenKY"
["gq_score"]=> string(1) "0"
["gq_ping"]=> string(2) "50"
}
[4]=> array(6) {
["frags"]=> string(1) "0"
["ping"]=> string(2) "63"
["nick"]=> string(4) "dupe"
["gq_name"]=> string(4) "dupe"
["gq_score"]=> string(1) "0"
["gq_ping"]=> string(2) "63"
}
[5]=> array(6) {
["frags"]=> string(1) "0"
["ping"]=> string(2) "72"
["nick"]=> string(7) "B2B vcc"
["gq_name"]=> string(7) "B2B vcc"
["gq_score"]=> string(1) "0"
["gq_ping"]=> string(2) "72"
}
[6]=> array(6) {
["frags"]=> string(1) "0"
["ping"]=> string(2) "82"
["nick"]=> string(10) "[dB]tumble"
["gq_name"]=> string(10) "[dB]tumble"
["gq_score"]=> string(1) "0"
}
}
答案 2 :(得分:1)
这是我用来从多维数组构建数组的基本函数。
function db_result_array($result, $key_column = null) {
for ($array = array(); $row = mysql_fetch_assoc($result); isset($row[$key_column]) ? $array[$row[$key_column]] = $row : $array[] = $row);
return $array;
}
答案 3 :(得分:1)
你这样做:
$promodplist = $data['promod']['players'];
foreach($promodplist as $k => $v)
var_dump($promodplist);
表示执行相同的var_dump()30次。您不需要代码中的foreach(),这样做:
$promodplist = $data['promod']['players'];
var_dump($promodplist);
作为一个单独的注释,你可以做的另一件事是:
$promodplist = $data['promod']['players'];
echo "<pre>";
print_r($promodplist);
echo "</pre>";