Skip to content
Advertisement

Dont print empty json arrays in php

Im programming a website where I can query server details from a game server. The problem is, that Ark sometimes prints empty playernames. That happens because there connecting at this moment or because they’re bots. I dont want to show them on my website. The problem is, that i don’t know how to exclude them. Maybe somebody can help me. It prints something like this:

Never Sober: 00h:05m:11s

kishko: 00h:05m:03s

FarmersmurfX: 00h:01m:47s

Furiousdiamon3: 00h:01m:21s

: 00h:00m:00s

: 00h:00m:00s

: 00h:00m:00s

And I dont want the last three to be shown.

I use this to display the players on my website and convert the seconds to time:

<?php 
foreach ($serverstatus->players as $player) {
 echo('<h5 class="dark">');
 $rawtimeconv = $player->raw->time;
 $rawtimeconv = round($rawtimeconv);
 $output = sprintf('%02dh:%02dm:%02ds', ($rawtimeconv/ 3600),($rawtimeconv/ 60 % 60), $rawtimeconv% 60);
 echo $player->name . ": ";
 echo $output;
 echo "</h5><br>";
}
?>
And this is my Json file.

```{
   "name":"MTSArk.co.uk [ARENA] Deathmatch - (v344.3)",
   "map":"ArenaModMap",
   "password":false,
   "raw":{
      "protocol":17,
      "folder":"ark_survival_evolved",
      "game":"ARK: Survival Evolved",
      "appId":346110,
      "numplayers":72,
      "numbots":0,
      "listentype":"d",
      "environment":"w",
      "secure":1,
      "version":"1.0.0.0",
      "steamid":"90156878733671434",
      "tags":[
         "",
         "OWNINGID:90156878733671434",
         "OWNINGNAME:90156878733671434",
         "NUMOPENPUBCONN:20",
         "P2PADDR:90156878733671434",
         "P2PPORT:7800",
         "LEGACY_i:0"
      ]
   },
   "maxplayers":70,
   "players":[
      {
         "name":"123",
         "raw":{
            "score":0,
            "time":5368.2802734375
         }
      },
      {
         "name":"Happy_Ghost",
         "raw":{
            "score":0,
            "time":134.0287322998047
         }
      },
      {
         "name":"123",
         "raw":{
            "score":0,
            "time":124.54788970947266
         }
      },
      {
         "name":"᠌",
         "raw":{
            "score":0,
            "time":77.85694885253906
         }
      },
      {
         "name":"",
         "raw":{
            
         }
      },
      {
         "name":"",
         "raw":{
            
         }
      }
   ],
   "bots":[
      
   ],
   "connect":"85.190.148.87:7800",
   "ping":47
}```

Advertisement

Answer

To skip empty names, add this at the beginning of your foreach loop :

if(!strlen($player->name))
    continue;
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement