-1

I am getting this response from an API:

{"players":224

Is there something simple I can do to condense it into 224, the number only? Thanks, any help would be much appreciated!

Code:

<?php 
$data = file_get_contents('http://api.iamphoenix.me/players/?server_ip=play.meloncraft.com&clean=true');
$array = explode(',', $data);
echo $array[0];
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
AFatTick
  • 3
  • 2

4 Answers4

1

Since $data is a JSON string, you need to decode it with json_decode rather than just split it with explode. However, json_decode will normally return an object. If you want it to return an associative array, use true as the second argument:

<?php 
$data = file_get_contents('http://api.iamphoenix.me/players/?server_ip=play.meloncraft.com&clean=true');
$array = json_decode($data, true);
echo $array["players"]; // will output "214"
?>
0

Lol ye that is JSON,

Search it up if you're intrested. Anyway:

json_decode($data);

This will convert it into a php array.

user3152069
  • 408
  • 3
  • 9
0

Just use: json_decode on the result, and see what it will give You :)

Bartosz Grzybowski
  • 1,149
  • 8
  • 18
0

You're receiving data in JSON format. You can decode it and extract the data you want like this:

$myData = json_decode($data);
$playerCount = $myData['players'];    // 224