How do I iterate through this multi array?

Hi there everyone!

I need help figuring out how to get the user’s data from this array. I’m able to get the player_count and status ($pj[‘player_count’]) but I’m having problem figuring out how to build a loop to get the player data in that subarray.

How would I iterate through $pj[‘1’] all the way through until it’s done?

Array
(
    [player_count] => 28
    [status] => 1
    [1] => Array
        (
            [player_discord] => discord:244532516357144576
            [player_live] => 
            [player_license] => license:7ecf6a3b3e24b81e882932b1ab8842fbaf6d29a7
            [player_steam] => steam:110000136f9ed7b
            [player_endpoint] => dddddd4
            [player_ping] => 42
            [player_id] => 595
            [player_name] => bloodaspectgaming
        )

    [2] => Array
        (
            [player_discord] => discord:179700881275486208
            [player_live] => 
            [player_license] => license:4ada7418d18bfac3851e380e8a7b03bbefb34f93
            [player_steam] => steam:110000109b620fe
            [player_endpoint] => dddddd
            [player_ping] => 138
            [player_id] => 505
            [player_name] => Tezy
        )

    [3] => Array
        (
            [player_discord] => discord:307144465456431104
            [player_live] => 
            [player_license] => license:c4179916975597f93ec7caffacda1ac3be96dc27
            [player_steam] => steam:1100001179fe6af
            [player_endpoint] => ddddd
            [player_ping] => 137
            [player_id] => 581
            [player_name] => Hs99
        )

    [4] => Array
        (
            [player_discord] => discord:304336678376570890
            [player_live] => live:914801526973294
            [player_license] => license:3b713509b93f0c99979f1843e56cd635770498ea
            [player_steam] => steam:11000011b899440
            [player_endpoint] => dddd
            [player_ping] => 76
            [player_id] => 583
            [player_name] => RattyNine
        )

    [5] => Array
        (
            [player_discord] => discord:369580760258183179
            [player_live] => live:844427526308587
            [player_license] => license:dcfa31a70cca376af46b87fbf041c0eaa720d78c
            [player_steam] => steam:1100001040629a0
            [player_endpoint] =>  ddddddd
            [player_ping] => 43
            [player_id] => 561
            [player_name] => 426-griffin.totty
        )

Assuming player_count is an accurate measure of the number of player records enclosed in the array, how would you iterate through it?

player_count should definitely be the number of players online. I tried a

while($array[i]){
//stuff
$i=++$i;
}

But that did not work. I also tried that same structure with a foreach instead but that also didn’t work for me.

I remember reading somewhere about iterating a set amount but I can’t seem to find it again. I’ll keep digging, however :slight_smile:

Hey, I think I found it!

$playersJson = playersJson($server_host.':'.$server_port);
for ($i = 0; $i < $playersJson['player_count']; $i++) {

print_r($playersJson[$i]);

}

Does that look like what I should be doing?

Alllmost. Two very minor tweaks.

The player numbering (at least, from the example you showed) starts from 1, rather than 0. And the final player number would be equal to the player count.

1 Like

I should have mentioned that I did get an illegal offset error so I switched the start of my playercount to 0 instead of 1, since that seems to be by far the most popular way of doing it. I made my operator change based on your tip, I figured I could just increase the limiter number by one or handle it the way I did.

$playersJson = playersJson($server_host.':'.$server_port);
for ($i = 0; $i <= $playersJson['player_count']; $i++) {

	//print_r($playersJson[$i]);

}

Try the following because it adds line feeds and the content can be read more easily.

echo '<pre>';
print_r( $playersJson['player_count'] );
echo '</pre>';
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.