Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Data from JSON URL

Tags:

json

php

parsing

I'm trying to get data onto my website by parsing data from a JSON format. The URL is http://api.bfhstats.com/api/onlinePlayers and I'm trying to output for example the currently players online on PC.

Here's the current code I have:

<?$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data = json_decode($jsondata, true);
echo $data->pc->peak24;?>

I thought this would work, however it's not displaying anything. I am very new to parsing JSON data so if someone could explain what I'm doing wrong that would be brilliant.

like image 276
Stewartps Avatar asked Dec 10 '25 01:12

Stewartps


1 Answers

You are missing the foreach cycle to fetch the two dimensional array $data:

<?php
$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data=array();
$data = json_decode($json, true);
//print_r ($data);
foreach ($data as $pc) { 
    echo $pc["peak24"]."<br>";
}
?>

Check the $json and $jsondata that have different name but should be the same.

like image 78
kiks73 Avatar answered Dec 12 '25 13:12

kiks73



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!