I have a json object like below. I need to locate and load the "data" value in each variable for further processing. I went through few basic PHP examples here, but didn't work. Any help is greatly appreciated.
{
"dataset":
{
"id":9775409,
"dataset_code":"AAPL",
"database_code":"WIKI",
"name":"Apple Inc (AAPL) Prices, Dividends, Splits and Trading Volume",
"description":"End of day open, high, low, close and volume, dividends and splits, and split/dividend adjusted open, high, low close and volume for Apple Inc. (AAPL). Ex-Dividend is non-zero on ex-dividend dates. Split Ratio is 1 on non-split dates. Adjusted prices are calculated per CRSP (\u003ca href=\"http://www.crsp.com/products/documentation/crsp-calculations\" rel=\"nofollow\" target=\"blank\"\u003ewww.crsp.com/products/documentation/crsp-calculations\u003c/a\u003e)
\u003cp\u003eThis data is in the public domain. You may copy, distribute, disseminate or include the data in other products for commercial and/or noncommercial purposes.\u003c/p\u003e
\u003cp\u003eThis data is part of Quandl's Wiki initiative to get financial data permanently into the public domain. Quandl relies on users like you to flag errors and provide data where data is wrong or missing. Get involved: \u003ca href=\"mailto:connect@quandl.com\" rel=\"nofollow\" target=\"blank\"\u003econnect@quandl.com\u003c/a\u003e",
"refreshed_at":"2017-11-03T21:50:44.247Z",
"newest_available_date":"2017-11-03",
"oldest_available_date":"1980-12-12",
"column_names":["Date","Open","High","Low","Close","Volume","Ex-Dividend","Split Ratio","Adj. Open","Adj. High","Adj. Low","Adj. Close","Adj. Volume"],
"frequency":"daily",
"type":"Time Series",
"premium":false,"
limit":null,
"transform":null,
"column_index":null,
"start_date":"2017-11-03",
"end_date":"2017-11-03",
"data":[["2017-11-03",174.0,174.26,171.12,172.5,58683826.0,0.0,1.0,174.0,174.26,171.12,172.5,58683826.0]],
"collapse":null,
"order":"asc",
"database_id":4922}
}
Here is the final code:
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$url ='https://www.quandl.com/api/v3/datasets/WIKI/CNK.json?start_date=2017-11-03&end_date=2017-11-03&order=asc&transformation=rdiff&api_key=xxxx';
$content = file_get_contents($url);
$json = json_decode($content, true);
$name = $json['dataset']['name'];
$str_pos = strpos($name,"(");
$closing_price = $json['dataset']['data'];
echo 'Name '.substr($name,0, $str_pos).'<br/>';
echo 'Closing price '.$closing_price[0][4].'<br/>';
?>
You can treat a json
object as you would with an array; you can loop through the json
or just call individual files.
<?php
$json_source = ''; # your json source
# two parameters:
# 1st - the source of the json file
# 2nd - if set to true it will set the array as an associative array
$json = json_decode($json_resource, true);
print_r($json['database']['data']);
To read more about json_decode
check out the manual.
This will output the json values from the data section. To assign value to each variable you can loop through the json:
foreach( $json['database']['data'] as $value )
{
$data[] = $value;
}
This will create a new array called $data
. You know would have something like this as the output:
Array
(
[0] => 2017-11-03
[1] => ...
)
Hope that helps!