Echo multidimensional array

I have a multidimensional player list array for Call of Duty 4. When I try to repeat the array, it returns with Array 30 times, because there are 30 players on the server.

Var_Dump of $ promodplist (Player 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 current players.

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

I just want to respond with nick (alias) in each array.

+4
source share
4 answers
 $promodplist = $data['promod']['players']; foreach($promodplist as $k => $v) print($v['nick']); 

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

+8
source

How about a function like this

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

In addition, formatting the array helps us understand what you are working with; one long string is much more difficult to decrypt than:

 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
source

Here is the basic function that I use to create arrays from multidimensional arrays.

 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; } 
+1
source

You do:

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

which means that you execute the same var_dump () file 30 times. You do not need foreach () in your code, this will do:

 $promodplist = $data['promod']['players']; var_dump($promodplist); 

As a separate note, you can do the following:

 $promodplist = $data['promod']['players']; echo "<pre>"; print_r($promodplist); echo "</pre>"; 
+1
source

All Articles