Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

php - Echo a multi dimensional array

I have an multidimensional array of a player list for Call of Duty 4. When I try to echo the array it comes back with Array 30 times because there are 30 current players in the server.

Var_Dump of $promodplist (Players List)

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)

And this is what I am trying to use to list the current players.

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

I just simply want to echo out the nick (nickname) in each array.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$promodplist = $data['promod']['players'];
foreach($promodplist as $k => $v)
    print($v['nick']);

Should do what you want. foreach iterates through the key/value pairs in the array, where $k is the element's key (a 0-based index, in your case) and $v is the value (an array of player data, for you). You can access the rest of the information by using its name as the key in the array accessor.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...