2012-08-06 84 views
4

我有一個多維數組,用於使命召喚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(暱稱)。

回答

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

應該做你想做的。 foreach遍歷數組中的鍵/值對,其中$k是元素的鍵(基於0的索引,在您的情況下),$v是值(您的玩家數據數組)。您可以通過使用其名稱作爲array accessor中的密鑰來訪問其餘的信息。

+0

bkconrad的解決方案是正確的,以做你想做的事。要在PHP中打印出任何數組(包括多維數組)的內容,請使用[print_r()] [print]函數。 [print]:http://php.net/manual/en/function.print-r.php – 2012-08-06 03:51:15

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; 
} 
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" 
    } 
} 
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>";