В настоящее время я работаю над модулем Joomla и в качестве альтернативы прекращенному финансированию Yahoo я решил использовать API из ietrading , например, который дает данные, как показано ниже:
пример извлечения: читаемый вывод 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
}
}
}
Для меня проблема заключается в том, как получить доступ к данным цитаты в каждом индексе с помощью следующего вывода:
AAPL : Apple Inc.
FB : Facebook Inc.
Единственный раз, когда это сработало, если я сделаю что-то вроде этого:
$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/>';
Я считаю, что есть оптимизированный способ сделать это.
print_r ($ json) в соответствии с рекомендациями
`'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 ) ) )'`