John Davidson

Dont print empty json arrays in php

0 comments
Message:


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
}




My Answer:
You can filter out the empty player names before displaying them on your website. Here's an example of how you can achieve this in PHP:

php
$players = [
"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"
];

foreach ($players as $player) {
$playerInfo = explode(": ", $player);
$playerName = $playerInfo[0];
$playTime = $playerInfo[1];

if ($playerName != "") {
echo $playerName . ": " . $playTime . "
";
}
}


This code will only display player names that are not empty. You can adjust the logic as needed to fit your specific requirements.

Rate this post

3 of 5 based on 5150 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us