-1

I am new to JSON and I am facing some difficulties , i am doing an echo $response; and i am getting:

    { "items": [ { "tableName": "CO.Affected_Country", "count": 1, "columnNames": [ "id" ], "rows": [ [ "12" ] ] } ], "links": [ { "rel": "self", "href": "https://ter.ge.com/services/rest/connect/v1.3/queryResults?query=x" }, { "rel": "canonical", "href": "x" }, { "rel": "describedby", "href": "x", "mediaType": "application/schema+json" } ] } 

i am trying to get the 'row' value , so i am trying to use $response->{"items"}->{"row"}; I know this syntax is wrong , but how can i do it?

Walid
  • 71
  • 1
  • 10

5 Answers5

1
$obj = json_decode($response);
$rows = $obj->items[0]->rows[0];

This is an array. To get the first value (12), you can then do:

echo $rows[0]

Live demo

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
1

Try This

$data = json_decode($response);
echo $data["items"][0]["tableName"];
Understand
  • 11
  • 1
0

Use json_decode($response) so:

$data = json_decode($response);
$data->items->row; // or $data['items']['row']
li0n_za
  • 455
  • 2
  • 15
0

Use json_decode($response) to use json as an object

$data = json_decode($response);

or if you want to use it as array, put true after reponse variable

$data = json_decode($response, true);
codeGig
  • 1,024
  • 1
  • 8
  • 11
0

try this

$data = json_decode($response, true);

echo $data["items"][0]["tableName"];
JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Aug 05 '16 at 13:49