如何使用php从json获取数组

时间:2012-05-11 06:07:05

标签: php json

我一直想知道这个Yahoo API大约一个月。我得到了yql并获取了股票价格的数据。

我的数组看起来像这样:

Array
(
    [query] => Array
    (
        [count] => 5
        [created] => 2012-05-11T01:14:55Z
        [lang] => en-US
        [diagnostics] => Array
        (
            [publiclyCallable] => true
            [redirect] => Array
            (
                [from] => http://datatables.org/alltables.env
                [status] => 301
                [content] => http://www.datatables.org/alltables.env
            )

            [url] => Array
            (
                [0] => Array
                (
                    [execution-start-time] => 425
                    [execution-stop-time] => 865
                    [execution-time] => 440
                    [proxy] => DEFAULT
                    [content] => http://datatables.org/alltables.env
                )

                [1] => Array
                (
                    [execution-start-time] => 884
                    [execution-stop-time] => 1097
                    [execution-time] => 213
                    [proxy] => DEFAULT
                    [content] => http://www.datatables.org/yahoo/finance/yahoo.finance.quotes.xml
                )

                [2] => Array
                (
                    [execution-start-time] => 1307
                    [execution-stop-time] => 1307
                    [execution-time] => 0
                    [proxy] => DEFAULT
                    [content] => http://download.finance.yahoo.com/d/quotes.csv?f=aa2bb2b3b4cc1c3c6c8dd1d2ee1e7e8e9ghjkg1g3g4g5g6ii5j1j3j4j5j6k1k2k4k5ll1l2l3mm2m3m4m5m6m7m8nn4opp1p2p5p6qrr1r2r5r6r7ss1s7t1t7t8vv1v7ww1w4xy&s=XAUUSD%3DX,XAGUSD%3DX,PLN12.NYM,PAK12.NYM,USDCAD%3DX
                )
            )

            [query] => Array
            (
                [execution-start-time] => 1105
                [execution-stop-time] => 1308
                [execution-time] => 203
                [params] => {url=[http://download.finance.yahoo.com/d/quotes.csv?f=aa2bb2b3b4cc1c3c6c8dd1d2ee1e7e8e9ghjkg1g3g4g5g6ii5j1j3j4j5j6k1k2k4k5ll1l2l3mm2m3m4m5m6m7m8nn4opp1p2p5p6qrr1r2r5r6r7ss1s7t1t7t8vv1v7ww1w4xy&s=XAUUSD%3DX,XAGUSD%3DX,PLN12.NYM,PAK12.NYM,USDCAD%3DX]}
                [content] => select * from csv where url=@url and columns='Ask,AverageDailyVolume,Bid,AskRealtime,BidRealtime,BookValue,Change&PercentChange,Change,Commission,ChangeRealtime,AfterHoursChangeRealtime,DividendShare,LastTradeDate,TradeDate,EarningsShare,ErrorIndicationreturnedforsymbolchangedinvalid,EPSEstimateCurrentYear,EPSEstimateNextYear,EPSEstimateNextQuarter,DaysLow,DaysHigh,YearLow,YearHigh,HoldingsGainPercent,AnnualizedGain,HoldingsGain,HoldingsGainPercentRealtime,HoldingsGainRealtime,MoreInfo,OrderBookRealtime,MarketCapitalization,MarketCapRealtime,EBITDA,ChangeFromYearLow,PercentChangeFromYearLow,LastTradeRealtimeWithTime,ChangePercentRealtime,ChangeFromYearHigh,PercebtChangeFromYearHigh,LastTradeWithTime,LastTradePriceOnly,HighLimit,LowLimit,DaysRange,DaysRangeRealtime,FiftydayMovingAverage,TwoHundreddayMovingAverage,ChangeFromTwoHundreddayMovingAverage,PercentChangeFromTwoHundreddayMovingAverage,ChangeFromFiftydayMovingAverage,PercentChangeFromFiftydayMovingAverage,Name,Notes,Open,PreviousClose,PricePaid,ChangeinPercent,PriceSales,PriceBook,ExDividendDate,PERatio,DividendPayDate,PERatioRealtime,PEGRatio,PriceEPSEstimateCurrentYear,PriceEPSEstimateNextYear,Symbol,SharesOwned,ShortRatio,LastTradeTime,TickerTrend,OneyrTargetPrice,Volume,HoldingsValue,HoldingsValueRealtime,YearRange,DaysValueChange,DaysValueChangeRealtime,StockExchange,DividendYield'
                )

                [javascript] => Array
                (
                    [execution-time] => 266
                    [instructions-used] => 302754
                    [table-name] => yahoo.finance.quotes
                )

                [warning] => Unable to sort response according to field 'Ask,Bid,AskRealtime,BidRealtime,LastTradeDate,LastTradePriceOnly,LastTradeTime'.
                [user-time] => 1373
                [service-time] => 653
                [build-version] => 27222
            )

            [results] => Array
            (
                [quote] => Array
                (
                    [0] => Array
                    (
                        [symbol] => XAUUSD=X
                        [Bid] => 1590.9498
                        [AskRealtime] => 
                        [BidRealtime] => 
                        [LastTradeDate] => 5/11/2012
                        [LastTradePriceOnly] => 1590.5499
                        [LastTradeTime] => 9:12pm
                        [Ask] => 1590.15
                    )

                    [1] => Array
                    (
                        [symbol] => XAGUSD=X
                        [Bid] => 
                        [AskRealtime] => 
                        [BidRealtime] => 
                        [LastTradeDate] => 5/10/2012
                        [LastTradePriceOnly] => 29.136
                        [LastTradeTime] => 5:27pm
                        [Ask] => 
                    )

                    [2] => Array
                    (
                        [symbol] => PLN12.NYM
                        [Bid] => 
                        [AskRealtime] => 1486.50
                        [BidRealtime] => 1486.00
                        [LastTradeDate] => 5/11/2012
                        [LastTradePriceOnly] => 1486.00
                        [LastTradeTime] => 8:44pm
                        [Ask] => 
                    )

                    [3] => Array
                    (
                        [symbol] => PAK12.NYM
                        [Bid] => 
                        [AskRealtime] => 616.00
                        [BidRealtime] => 609.40
                        [LastTradeDate] => 5/11/2012
                        [LastTradePriceOnly] => 658.80
                        [LastTradeTime] => 8:00pm
                        [Ask] => 
                    )

                    [4] => Array
                    (
                        [symbol] => USDCAD=X
                        [Bid] => 1.0033
                        [AskRealtime] => 
                        [BidRealtime] => 
                        [LastTradeDate] => 5/11/2012
                        [LastTradePriceOnly] => 1.0034
                        [LastTradeTime] => 9:13pm
                        [Ask] => 1.0035
                    )
                )
            )
        )
    )

我想获取以下数据:

Ask
Bid
AskRealtime
BidRealtime
LastTradeDate
LastTradePriceOnly
LastTradeTime

从数组中将其打印在自动刷新的html表中:

<table width="200" border="1">
  <tr>
    <td>Ask</td>
    <td>Value</td>
  </tr>
  <tr>
    <td>BID</td>
    <td>Value</td>
  </tr>
  <tr>
    <td>AskRealime</td>
    <td>Value</td>
  </tr>
  <tr>
    <td>BidRealtime</td>
    <td>Value</td>
  </tr>
  <tr>
    <td>LastTradeDate</td>
    <td>Value</td>
  </tr>
  <tr>
    <td>LastTradePriceOnly</td>
    <td>Value</td>
  </tr>
  <tr>
    <td>LastTradeTime</td>
    <td>Value</td>
  </tr>
</table>

任何建议?

1 个答案:

答案 0 :(得分:1)

看起来你可以访问你想要的数据并循环通过它:

foreach ($array['query']['results']['quote'] as $quote) {
    echo $quote['Ask'];
    echo $quote['Bid'];
    ...
}

$array是此数据所属变量的名称。我将把HTML的结构留给你。