Home > Back-end >  PHP api call, but format is strange
PHP api call, but format is strange

Time:03-13

JSON Return (Image)

What to do? since if i parse with json i only get the "xx.xx" values

<?php
  header('Access-Control-Allow-Origin: *');
  $url = "https://www.henrikhjelm.se/api/getdata.php?lan=kronobergs-lan";
  $json = file_get_contents($url);
  $json_data = json_decode($json, true);
  
  foreach ($json_data as $value) {
    echo "Value: $value <br>";
  }

?>

this is the code I use, I only get the values that are in "" from the api. But I need both the first value and the value inside "". Don't know how to do it.

var_dump($json)

string(8728) "{"kronobergslan_Ingo_VaxjoSmedjegatan_28__Morners_vag__biodiesel": "34.23","kronobergslan_OKQ8_VaxjoStora_Vagen_6__biodiesel": "10.04","kronobergslan_OKQ8_VaxjoArabygatan_53__etanol": "19.24","kronobergslan_Tanka_MarkarydHassleholmsvagen_2__etanol": "19.24","kronobergslan_Preem_VaxjoOstregardsgatan_51__etanol": "19.24","kronobergslan_OKQ8_MarkarydGlasbruksvagen_15_Stromsnasbruk__etanol": "19.24","kronobergslan_Tanka_AlvestaBladingevagen_20__etanol": "19.24","kronobergslan_OKQ8_LjungbyHelsingborgsvagen__etanol": "19.24","kronobergslan_Tanka_LjungbyNyponvagen_7__etanol": "19.24","kronobergslan_OKQ8_LesseboStorgatan_60__etanol": "19.24","kronobergslan_St1_VaxjoVingavagen_1__etanol": "19.14","kronobergslan_St1_VaxjoMorners_Vag_2__etanol": "19.14","kronobergslan_Ingo_VaxjoSoderleden_2__etanol": "19.14","kronobergslan_Ingo_LjungbyHarabergsgatan__etanol": "19.14","kronobergslan_St1_LjungbyMarta_Ljungbergsvagen_56__etanol": "19.14","kronobergslan_St1_MarkarydDrottninggatan_50__etanol": "19.14","kronobergslan_Ingo_VaxjoSmedjegatan_28__Morners_vag__etanol": "34.23","kronobergslan_Preem_TingsrydLokgatan__etanol": "19.41","kronobergslan_Preem_MarkarydUlvarydsvag__etanol": "19.39","kronobergslan_Circle_K_VaxjoSodra_Vallviksvagen_352_51_Vaxjo__etanol": "19.39","kronobergslan_Circle_K_LjungbyRingvagen_1__etanol": "19.39","kronobergslan_Preem_UppvidingeJarnvagsgatan_Aseda__etanol": "19.39","kronobergslan_Preem_VaxjoVarnamovagen_Lammhult__etanol": "19.39","kronobergslan_Circle_K_UppvidingeVaxjovagen_Lenhovda__etanol": "19.39","kronobergslan_Preem_VaxjoSandviksvagen__etanol": "19.39","kronobergslan_Circle_K_VaxjoJonkopingsvagen_14_Lammhult__etanol": "19.39","kronobergslan_OKQ8_VaxjoSambandsvagen_2__etanol": "19.39","kronobergslan_Preem_LjungbyLanggatan_2__etanol": "19.39","kronobergslan_Circle_K_LjungbyLagan__etanol": "19.39","kronobergslan_OKQ8_VaxjoNorremarksvagen_2__etanol": "19.39","kronobergslan_Circle_K_VaxjoHejaregatan_17__etanol": "19.39","kronobergslan_Circle_K_VaxjoEvedalsvagen__etanol": "19.39","kronobergslan_OKQ8_UppvidingeAspvagen_9_Alstermo__etanol": "19.39","kronobergslan_Preem_AlmhultS_Esplanaden__etanol": "19.24","kronobergslan_OKQ8_AlvestaFabriksgatan_3__etanol": "19.24","kronobergslan_Tanka_AlmhultDanska_Vagen_4__etanol": "19.24","kronobergslan_OKQ8_Almhultalmhult_ringvagen_10__etanol": "19.24","kronobergslan_dinX_TingsrydSodra_vagen_19_urshult__etanol": "19.24","kronobergslan_OKQ8_AlmhultSodra_Ringvagen_10__etanol": "19.24","kronobergslan_Tanka_VaxjoLjungadalsgatan_1__etanol": "19.24","kronobergslan_Preem_LesseboStorgatan_56a__etanol": "19.24","kronobergslan_Preem_VaxjoRadjursvagen__etanol": "19.24","kronobergslan_OKQ8_VaxjoStora_Vagen_6__etanol": "19.24","kronobergslan_Tanka_TingsrydStorgatan_113__etanol": "19.24","kronobergslan_Preem_VaxjoHammerdalsvagen_3__etanol": "19.24","kronobergslan_Tanka_AlvestaBladingevagen_20__diesel": "24.57","kronobergslan_OKQ8_LjungbyHelsingborgsvagen__diesel": "24.57","kronobergslan_Tanka_LjungbyNyponvagen_7__diesel": "24.57","kronobergslan_OKQ8_LesseboStorgatan_60__diesel": "24.57","kronobergslan_Ingo_VaxjoSoderleden_2__diesel": "24.53","kronobergslan_St1_VaxjoVingavagen_1__diesel": "24.47","kronobergslan_St1_VaxjoMorners_Vag_2__diesel": "24.47","kronobergslan_Ingo_LjungbyHarabergsgatan__diesel": "24.47","kronobergslan_St1_LjungbyMarta_Ljungbergsvagen_56__diesel": "24.47","kronobergslan_St1_MarkarydDrottninggatan_50__diesel": "24.47","kronobergslan_dinX_TingsrydSodra_vagen_19_urshult__diesel": "24.46","kronobergslan_OKQ8_VaxjoStora_Vagen_6__diesel": "24.45","kronobergslan_Preem_VaxjoOstregardsgatan_51__diesel": "24.13","kronobergslan_Smart_Energy_LesseboSkruv_Lagervagen_1__diesel": "23.83","kronobergslan_Ingo_VaxjoSmedjegatan_28__Morners_vag__diesel": "100.00","kronobergslan_Tanka_VaxjoLjungadalsgatan_1__diesel": "52.39","kronobergslan_Circle_K_VaxjoSodra_Vallviksvagen_352_51_Vaxjo__diesel": "29.12",

var_dump($json_data)

array(141) { ["kronobergslan_Ingo_VaxjoSmedjegatan_28__Morners_vag__biodiesel"]=> string(5) "34.23" ["kronobergslan_OKQ8_VaxjoStora_Vagen_6__biodiesel"]=> string(5) "10.04" ["kronobergslan_OKQ8_VaxjoArabygatan_53__etanol"]=> string(5) "19.24" ["kronobergslan_Tanka_MarkarydHassleholmsvagen_2__etanol"]=> string(5) "19.24" ["kronobergslan_Preem_VaxjoOstregardsgatan_51__etanol"]=> string(5) "19.24" ["kronobergslan_OKQ8_MarkarydGlasbruksvagen_15_Stromsnasbruk__etanol"]=> string(5) "19.24" ["kronobergslan_Tanka_AlvestaBladingevagen_20__etanol"]=> string(5) "19.24" ["kronobergslan_OKQ8_LjungbyHelsingborgsvagen__etanol"]=> string(5) "19.24"

I'm trying to build a table with name "kronobergslan_Ingo_VaxjoSmedjegatan_28__Morners_vag__biodiesel" and value "34.23"

CodePudding user response:

Welcome to Stack Overflow community. First of all, you are looping through values only. You need to slightly modify your foreach loop to include keys. Here is how you do it:

<?php

$url = "https://www.henrikhjelm.se/api/getdata.php?lan=kronobergs-lan";
$json = file_get_contents($url);
$entries = json_decode($json, true);

foreach ($entries as $key => $value) {
    echo("key: $key<br>");
    echo("value: $value<br>");
    echo("<br>");
}

The output is the following:

key: kronobergslan_Ingo_VaxjoSmedjegatan_28__Morners_vag__biodiesel
value: 34.23

key: kronobergslan_OKQ8_VaxjoStora_Vagen_6__biodiesel
value: 10.04

key: kronobergslan_OKQ8_VaxjoArabygatan_53__etanol
value: 19.24

I hope this is the desired result. Let me know if you have any questions left.

  • Related