0

I'm getting a JSON file that has the following data:

{
    "Meta Data": {
        "1. Information": "Daily Prices (open, high, low, close) and Volumes",
        "2. Symbol": "GE",
        "3. Last Refreshed": "2018-12-14 16:00:04",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2018-12-14": {
            "1. open": "7.0800",
            "2. high": "7.2500",
            "3. low": "6.9950",
            "4. close": "7.1000",
            "5. volume": "128766603"
        },
        "2018-12-13": {
            "1. open": "7.4900",
            "2. high": "7.5000",
            "3. low": "7.1200",
            "4. close": "7.2000",
            "5. volume": "207038918"
        },
        "2018-12-12": {
            "1. open": "6.8400",
            "2. high": "7.0250",
            "3. low": "6.7000",
            "4. close": "6.7100",
            "5. volume": "105819951"
        },
        "2018-12-11": {
            "1. open": "7.0400",
            "2. high": "7.1300",
            "3. low": "6.6600",
            "4. close": "6.7600",
            "5. volume": "124580597"
        },
        "2018-12-10": {
            "1. open": "6.9700",
            "2. high": "7.1200",
            "3. low": "6.7500",
            "4. close": "6.9300",
            "5. volume": "112841778"
        }

I want to retrieve data in PHP file to Display Stock open, High, Low, Close, Volume in a table format as attached below. enter image description here

I tried:

$stockurl="http://alphavantage.co/…"; 
$json = file_get_contents($stockurl); 
$obj1 = json_decode($json); 
$obj =$obj1->{'Time Series (Daily)'}; 
$date = "2018-12-14"; 
$dataForSingleDate = $obj->{$date}; 
echo $dataForSingleDate->{'1. open'}.'<br/>'; 
echo $dataForSingleDate->{'2. high'}.'<br/>'; 
echo $dataForSingleDate->{'3. low'}.'<br/>'; 
echo $dataForSingleDate->{'4. close'}.'<br/>'; 
echo $dataForSingleDate->{'5. volume'}.'<br/>'; 

But i need to add date manualy and thats the probelm.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    What problem are you having? Call `json_decode()`, then loop over the array and print what you want. – Barmar Dec 15 '18 at 03:07
  • See [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – Barmar Dec 15 '18 at 03:08
  • Use `json_decode($json, true)` so you get an associative array. Then you can do `foreach ($obj1['Time Series (Daily)'] as $date => $row)` – Barmar Dec 15 '18 at 03:16

1 Answers1

0
$output = json_decode($your_json_array,'true'); 

this will return associative array objects so that you can loop through easily.

rene
  • 41,474
  • 78
  • 114
  • 152
Anoop P S
  • 754
  • 1
  • 12
  • 31