0

I'm downloading some json using the following:

$api_resp = json_decode(@file_get_contents($api_url),true);

Which give the following array (using var_dump):

array(4) {
  ["body"]=>
  array(1) {
    [1657134820]=>
    array(1) {
      [0]=>
      float(1.313)
    }
  }
  ["status"]=>
  string(2) "ok"
  ["time_exec"]=>
  float(0.031042098999023)
  ["time_server"]=>
  int(1657140220)
}

How can I access the 1.313 float value?

I'm able to access the body using $api_resp['body'], but how do I go from there? $api_resp['body'][0] gives "PHP Notice: Undefined offset: 0"

zcoop98
  • 2,590
  • 1
  • 18
  • 31
  • `$api_resp['body'][1657134820][0]` – Barmar Jul 06 '22 at 21:18
  • The body property has an array using the key `1657134820`. Probably that key is some id and is never the same number. If the body array always has 1 property, you can use php's [reset()](https://www.php.net/manual/en/function.reset.php) function. In your case: `$body = reset($api_resp["body"]);` – Leroy Jul 06 '22 at 21:20
  • Or just loop over `$api_resp['body']` – Barmar Jul 06 '22 at 21:21
  • Thanks @Leroy, that solved it. Using reset($api_resp["body"])[0] gives me the value. – Tom Widerøe Jul 07 '22 at 06:33

0 Answers0