4

我有一个使命召唤 4 的玩家列表的多维数组。当我尝试回显该数组时,它会返回Array30 次,因为服务器中有 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在每个数组中回显(昵称)。

4

4 回答 4

8
$promodplist = $data['promod']['players'];
foreach($promodplist as $k => $v)
    print($v['nick']);

应该做你想做的。foreach遍历数组中的键/值对,其中$k是元素的键(在您的情况下是从 0 开始的索引)和$v值(对您来说是玩家数据数组)。您可以通过使用其名称作为数组访问器中的键来访问其余信息。

于 2012-08-06T03:39:52.100 回答
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" 
    }
}
于 2012-08-06T04:08:16.470 回答
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; 
}
于 2012-08-06T03:53:47.573 回答
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>";
于 2012-08-06T05:01:29.360 回答