0

I tried to read this json result :

stdClass Object (
    [search-results] => stdClass Object
        (
            [opensearch:totalResults] => 1770323
            [opensearch:startIndex] => 0
            [opensearch:itemsPerPage] => 25
            [opensearch:Query] => stdClass Object
                (
                    [@role] => request
                    [@searchTerms] => gene
                    [@startPage] => 0
                )
            [link] => Array
                (
                    [0] => stdClass Object
                        (
                            [@_fa] => true
                            [@href] => http://api.els.com/content/search/scidir?start=0&count=25&query=gene
                            [@ref] => self
                            [@type] => application/json
                        )
                )
        )
    )

my code :

$json = file_get_contents($requete); // dans la variable json
$obj = json_decode($json);
echo $obj->search-results->link[0]; // (I tried also  : $obj->{'search-results'}->link[0];

code error :

Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';' in

I don't understand where is my fault, please, have you an idea ? Thanks Jean

Tobias F.
  • 1,050
  • 1
  • 11
  • 23

2 Answers2

0

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

Check here

you use $obj->search-results->link[0]; . I thinks search-results have problem change the - in json to _ (underline) and check it again.

Mr.Sun
  • 129
  • 4
0

If search-result is fixed object then you can try as :

$search = 'search-results'; //declare variable 
echo $obj->{$search}->link[0]; // (I tried also  : $obj->{'search-results'}->link[0];
Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33