1

I have a problem with parsing data in json exposed with strings that contain the character "-", for example "foo-opened". All strings containing the value "-" in the php file return value 0 or error.

FILE JSON:

{
  "overview": [
    {
      "foo": {
        "foo-opened": 0,
        "foo-total": 110,
        "foo-closed": 110
      }
    }
  ],
}

FILE PHP

<?php
header('Content-type: text/html; charset=UTF-8');
$url = "data.json";
$contents = file_get_contents($url);
$obj=json_decode($contents);
$FooTotal = $obj->overview[0]->foo->foo-opened;
$FooOpen = $obj->overview[0]->foo->foo-total;
$FooClosed = $obj->overview[0]->foo->foo-closed;                          
echo "</p><p>Foo total:" . $FooTotal . "</p><p>Foo open:" . $FooOpen . "</p><p>Foo closed:" . $FooClosed . "</p></p>";
 ?> 

Thank you in advance for your help!

hoi
  • 25
  • 8

1 Answers1

3

You need to access the prop via $obj->overview[0]->foo->{'foo-total'}

scrowler
  • 24,273
  • 9
  • 60
  • 92