PHP json array output

hello all, i have a script that produces json data based on players in an online gaming server and i need to parse this data into a readable format to display the "name"s of all player in the server. there can be upto 32 players.

json data =

{ "state":"OK", "count":"5", "datalist":[ { "rank":125, "clantag":"", "name":"xxx", "guid":"xxxxxxxxxxxxxxxxxxxxxxxxxx", "pbGuid":"xxxxxxxxxxxxxxxxxxxxxxxxxx", "teamId":1, "squadId":1, "sessionKills":0, "sessionDeaths":0, "sessionScore":0, "highPingKickValue":0, "ping":-1, "kit":{ "kit":"unkown", "weapons":[ "unkown", "unkown", "unkown" ], "gadgets":[ "unkown", "unkown", "unkown" ] }, "alive":true, "teamkills":0, "sessionPlaytime":0, "connecttime":1472236248, "lastswitchtime":1472236248, "lastForcedSwitchTime":0, "numberOfFocedSwitches":0, "numberOfSwitches":1, "ip":"000.000.000.000", "visits":13, "roundTimeSeconds":180, "type":0 }, { "rank":5, "clantag":"", "name":"xxx", "guid":"xxxxxxxxxxxxxxxxxxxxxxxxxx", "pbGuid":"xxxxxxxxxxxxxxxxxxxxxxxxxx", "teamId":2, "squadId":1, "sessionKills":0, "sessionDeaths":0, "sessionScore":750, "highPingKickValue":0, "ping":-1, "kit":{ "kit":"unkown", "weapons":[ "unkown", "unkown", "unkown" ], "gadgets":[ "unkown", "unkown", "unkown" ] }, "alive":true, "teamkills":0, "sessionPlaytime":0, "connecttime":1472236684, "lastswitchtime":1472236844, "lastForcedSwitchTime":0, "numberOfFocedSwitches":0, "numberOfSwitches":1, "ip":"000.000.000.000", "visits":0, "roundTimeSeconds":180, "type":0 }, { "rank":0, "clantag":"", "name":"xxx", "guid":"xxxxxxxxxxxxxxxxxxxxxxxxxx", "pbGuid":"xxxxxxxxxxxxxxxxxxxxxxxxxx", "teamId":1, "squadId":1, "sessionKills":0, "sessionDeaths":1, "sessionScore":0, "highPingKickValue":0, "ping":32, "kit":{ "kit":"unkown", "weapons":[ "unkown", "unkown", "unkown" ], "gadgets":[ "unkown", "unkown", "unkown" ] }, "alive":true, "teamkills":1, "sessionPlaytime":0, "connecttime":1472236835, "lastswitchtime":1472236889, "lastForcedSwitchTime":0, "numberOfFocedSwitches":0, "numberOfSwitches":1, "ip":"000.000.000.000", "visits":0, "roundTimeSeconds":180, "type":0 }, { "rank":2, "clantag":"", "name":"xxx", "guid":"xxxxxxxxxxxxxxxxxxxxxxxxxx", "pbGuid":"xxxxxxxxxxxxxxxxxxxxxxxxxx", "teamId":2, "squadId":1, "sessionKills":0, "sessionDeaths":0, "sessionScore":0, "highPingKickValue":0, "ping":-1, "kit":{ "kit":"unkown", "weapons":[ "unkown", "unkown", "unkown" ], "gadgets":[ "unkown", "unkown", "unkown" ] }, "alive":true, "teamkills":0, "sessionPlaytime":0, "connecttime":1472236992, "lastswitchtime":1472237057, "lastForcedSwitchTime":0, "numberOfFocedSwitches":0, "numberOfSwitches":1, "ip":"000.000.000.000", "visits":0, "roundTimeSeconds":180, "type":0 }, { "rank":5, "clantag":"", "name":"xxx", "guid":"xxxxxxxxxxxxxxxxxxxxxxxxxx", "pbGuid":"xxxxxxxxxxxxxxxxxxxxxxxxxx", "teamId":2, "squadId":1, "sessionKills":0, "sessionDeaths":0, "sessionScore":0, "highPingKickValue":0, "ping":-1, "kit":{ "kit":"unkown", "weapons":[ "unkown", "unkown", "unkown" ], "gadgets":[ "unkown", "unkown", "unkown" ] }, "alive":true, "teamkills":0, "sessionPlaytime":0, "connecttime":1472237052, "lastswitchtime":1472237088, "lastForcedSwitchTime":0, "numberOfFocedSwitches":0, "numberOfSwitches":1, "ip":"000.000.000.000", "visits":0, "roundTimeSeconds":180, "type":0 } ] }

what i am trying =
[php]foreach($json->datalist->name as $mydata)
{
echo $mydata->name . “\n”;
}[/php]

what i am getting =

<br /> <b>Warning</b>: Invalid argument supplied for foreach() in <b>/usr/www/aacts/public/rconnet/test.php</b> on line <b>25</b><br />

Now if i am the only one in the server i can do this and it works fine, unless more come in … but i want to output ALL names of players that are online.

[php]echo $json[‘datalist’][‘name’]."\n";[/php]

I assume you are using json_decode?

if so add an extra param to the function true. This will convert it to an array and not an object.

A php object can’t be used as an array unless it is extending the ArrayIterator

e.g
[php]
$json_array = json_decode($my_json_string, true);

foreach ($json_array[‘datalist’] as $player_details) {
echo $player_details[‘name’];
}

[/php]

You probably want to check the keys are available using isset() or somerthing before you try to iterate through the array

Sponsor our Newsletter | Privacy Policy | Terms of Service