0

I tried to get data from a JSON file, but it didn't work the way I wanted it to.

Here's the JSON content:

{
   "ok":true,
   "license":"CC BY 4.0 - https:\/\/creativecommons.tankerkoenig.de",
   "data":"MTS-K",
   "status":"ok",
   "stations":[
      {
         "id":"0d08b94c-6ece-4924-9c8a-205b971e9eff",
         "name":"AVIA M\u00fcnsingen",
         "brand":"AVIA",
         "street":"Haupstr.",
         "place":"M\u00fcnsingen",
         "lat":48.411,
         "lng":9.50876,
         "dist":0.1,
         "diesel":1.249,
         "e5":1.369,
         "e10":1.349,
         "isOpen":true,
         "houseNumber":"117",
         "postCode":72525
      }
   ]
}

Here's my code:

$url = "https://creativecommons.tankerkoenig.de/json/list.phplat=48.411&lng=9.508&rad=1&sort=dist&type=all&apikey=APIKEY";
$contents = file_get_contents($url);

$aviadiesel = json_decode($contents);

$preise = $aviadiesel->{'stations'};
echo $preise[diesel];

But with this code the Output looks like this:

Array

I would appreciate your help. I'm new to PHP, and Google couldn't help me as well so far.

jibsteroos
  • 1,366
  • 2
  • 7
  • 13
karudolf
  • 3
  • 2
  • 1
    `echo $preise[0]['diesel'];` – u_mulder Jan 26 '20 at 20:43
  • 1
    Arrays cannot be printed through the `echo` construct. You must use something like `var_dump()` or `print_r()` to inspect them. – Sherif Jan 26 '20 at 20:44
  • You can output the value for diesel (or any other property of the object) e.g.: `echo $preise[0]->diesel;`. A `var_dump($preise)` reveals you need to access properties of a `stdClass` object, i.e. use `->` notation. – jibsteroos Jan 26 '20 at 20:56

0 Answers0