Search code examples
phpjsonapijoomla

PHP: Accessing data within complex json


I am currently working on a Joomla module and as alternative to the discontinued yahoo finance, i had decided to use API from ietrading such as which gives data as showing below:

sample extract: readable output of print_r($json):

    {
    "AAPL": {
      "quote": {
        "symbol": "AAPL",
        "companyName": "Apple Inc.",
        "primaryExchange": "Nasdaq Global Select",
        "sector": "Technology",
        "calculationPrice": "tops",
        "open": 162.62,            
        ...
        "week52High": 183.5,
        "week52Low": 142.2,
        "ytdChange": -0.04610909853958216
      }
    },
    "FB": {
      "quote": {
        "symbol": "FB",
        "companyName": "Facebook Inc.",
        "primaryExchange": "Nasdaq Global Select",
        "sector": "Technology",
        "calculationPrice": "tops",
        "open": 160.07,
        "openTime": 1524663000827,
        "close": 159.69,
        "closeTime": 1524686400183,
        "high": null,
        "low": null,
        "latestPrice": 173.19,
        ...
        "week52High": 195.32,
        "week52Low": 144.4216,
        "ytdChange": -0.11977731231396754
      }
    }
}

The issue for me is how to access the data of quote within each index with output as follows:

AAPL : Apple Inc.
FB : Facebook Inc.

The only time it worked is if I do something like this:

$test_data = file_get_contents('data.json');    
$json = json_decode($test_data,true);

$jsonData[] = "";

foreach($json as $item){
    $allItems[] = $item;
}

var_dump($allItems);

for ($i=0; $i < COUNT($allItems); $i++) {
    $jsonData[]= $allItems[$i]['quote'];
    echo $jsonData[$i+1]['companyName'].' - from all for<br/>';
}
echo "<h2>Testing</h2>";
echo $jsonData[1]['symbol'].' : '.$jsonData[1]['companyName'].'<br/>';
echo $jsonData[2]['symbol'].' : '.$jsonData[2]['companyName'].'<br/>';

I believe there is an optimise way of doing this.

print_r($json) as adviced

`'Array ( [AAPL] => Array ( [quote] => Array ( [symbol] => AAPL [companyName] => Apple Inc. [primaryExchange] => Nasdaq Global Select [sector] => Technology [calculationPrice] => tops [open] => 162.62 [openTime] => 1524663000142 [close] => 163.65 [closeTime] => 1524686400487 [high] => [low] => [latestPrice] => 164.57 [latestSource] => IEX real time price [latestTime] => 9:35:46 AM [latestUpdate] => 1524749746099 [latestVolume] => 1354291 [iexRealtimePrice] => 164.57 [iexRealtimeSize] => 100 [iexLastUpdated] => 1524749746099 [delayedPrice] => 164.28 [delayedPriceTime] => 1524748852180 [previousClose] => 163.65 [change] => 0.92 [changePercent] => 0.00562 [iexMarketPercent] => 0.01142 [iexVolume] => 15466 [avgTotalVolume] => 32759551 [iexBidPrice] => 164.61 [iexBidSize] => 100 [iexAskPrice] => 165.07 [iexAskSize] => 100 [marketCap] => 835030319410 [peRatio] => 16.91 [week52High] => 183.5 [week52Low] => 142.2 [ytdChange] => -0.046109098539582 ) ) [FB] => Array ( [quote] => Array ( [symbol] => FB [companyName] => Facebook Inc. [primaryExchange] => Nasdaq Global Select [sector] => Technology [calculationPrice] => tops [open] => 160.07 [openTime] => 1524663000827 [close] => 159.69 [closeTime] => 1524686400183 [high] => [low] => [latestPrice] => 173.19 [latestSource] => IEX real time price [latestTime] => 9:35:55 AM [latestUpdate] => 1524749755278 [latestVolume] => 13276538 [iexRealtimePrice] => 173.19 [iexRealtimeSize] => 100 [iexLastUpdated] => 1524749755278 [delayedPrice] => 172.53 [delayedPriceTime] => 1524748852406 [previousClose] => 159.69 [change] => 13.5 [changePercent] => 0.08454 [iexMarketPercent] => 0.02455 [iexVolume] => 325939 [avgTotalVolume] => 48329785 [iexBidPrice] => 173.04 [iexBidSize] => 200 [iexAskPrice] => 173.22 [iexAskSize] => 100 [marketCap] => 502140357915 [peRatio] => 28.16 [week52High] => 195.32 [week52Low] => 144.4216 [ytdChange] => -0.11977731231397 ) ) )'`

Solution

  • here is a short method to do that :

    in this example , i'm using the foreach loop instead of loop with index . this make you iterate on your array object by object and filtering data as you want .

    <?php
            //Enter your code here, enjoy!
     $data = ' {
        "AAPL": {
          "quote": {
            "symbol": "AAPL",
            "companyName": "Apple Inc.",
            "primaryExchange": "Nasdaq Global Select",
            "sector": "Technology",
            "calculationPrice": "tops",
            "open": 162.62,            
            "week52High": 183.5,
            "week52Low": 142.2,
            "ytdChange": -0.04610909853958216
          }
        },
        "FB": {
          "quote": {
            "symbol": "FB",
            "companyName": "Facebook Inc.",
            "primaryExchange": "Nasdaq Global Select",
            "sector": "Technology",
            "calculationPrice": "tops",
            "open": 160.07,
            "openTime": 1524663000827,
            "close": 159.69,
            "closeTime": 1524686400183,
            "high": null,
            "low": null,
            "latestPrice": 173.19,
           
            "week52High": 195.32,
            "week52Low": 144.4216,
            "ytdChange": -0.11977731231396754
          }
        }
    }';
    
    $json = json_decode($data,true);
    
    foreach($json as $key  => $object){
        echo $key ." : ". $object['quote']['companyName']."</br>";
    }
    

    result :

    AAPL : Apple Inc.

    FB : Facebook Inc.

    i hope that help you .