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.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

$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.

Solution 2

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

Also, formatting your array helps us see what you’re working with; one long line is much harder to decipher 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" 
    }
}

Solution 3

Here is a basic function I use to build arrays from multi dimensional 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; 
}

Solution 4

You do:

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

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

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

As a separate note, another thing you can do is this:

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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply