BCLtd BCLtd - 3 months ago 6
JSON Question

JSON - PHP - Reading Key

I have the following code below, which I am trying to run to save the key in the json to a PHP varible. However, for some reason it's just not doing anything - although it seems my code is right, any ideas where I am going wrong?

<?php
$json = file_get_contents('url');
$data = json_decode($json,true);

$id = $data['s_id'];


echo($id);

?>


and the json from the external URL looks like this

[{"s_id":"20063"}]

Answer

You have an array wrapping the object

$id = $data[0]['s_id'];