簡體   English   中英

在多維PHP數組中訪問信息

[英]Accessing information in multidimensional PHP array

我正在嘗試為顯示屏格式化一些數據。 我正在使用國家鐵路實時出發委員會Web服務搜索從我們本地車站發車的火車。

我正在使用一個名為OpenLDBWS.php的php腳本(在github上為h / t RailAleFan)連接到LDBWS。 該連接工作正常,但為了顯示我想要的結果,我在努力訪問它生成的數組。

所以這段代碼:

  <?php
require 'OpenLDBWS.php';
$OpenLDBWS = new OpenLDBWS("MY LDBWS API KEY HERE");
$result = $OpenLDBWS->GetDepartureBoard(1, "BHM");
header ("Content-Type: text/plain");
print_r($result);
?>

將產生以下結果:

     stdClass Object
(
    [GetStationBoardResult] => stdClass Object
        (
            [generatedAt] => 2016-02-03T16:11:20.1570854+00:00
            [locationName] => Birmingham New Street
            [crs] => BHM
            [nrccMessages] => stdClass Object
                (
                    [message] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [_] => <P>Disruption&nbsp;between Carlisle and&nbsp;Glasgow Central / Edinburgh. More details can be found in <A href="http://nationalrail.co.uk/service_disruptions/117252.aspx">Latest Travel News</A>.</P>
                                )

                            [1] => stdClass Object
                                (
                                    [_] => Disruption between Milton Keynes Central and London Euston / Clapham Junction. More details can be found in <A href="http://nationalrail.co.uk/service_disruptions/119008.aspx">Latest Travel News. </A>
                                )

                        )

                )

            [platformAvailable] => 1
            [trainServices] => stdClass Object
                (
                    [service] => stdClass Object
                        (
                            [std] => 16:05
                            [etd] => 16:09
                            [platform] => 8
                            [operator] => London Midland
                            [operatorCode] => LM
                            [serviceType] => train
                            [serviceID] => m+0v++xnRsACKmZqgUZwAg==
                            [origin] => stdClass Object
                                (
                                    [location] => stdClass Object
                                        (
                                            [locationName] => Longbridge
                                            [crs] => LOB
                                        )

                                )

                            [destination] => stdClass Object
                                (
                                    [location] => stdClass Object
                                        (
                                            [locationName] => Lichfield City
                                            [crs] => LIC
                                        )

                                )

                        )

                )

        )

)

我真正想要的是打折大量顯示某些字段的信息,例如[std] [etd]等。

我被困住是因為我知道信息是一個數組,但是我似乎無法獲得內容來開始打印所需的信息。 如果我取出print_r並用回聲“ $ result”替換它,它將僅顯示一個空白屏幕。

我意識到這是一個非常初學者的問題,我可能會遺漏一些顯而易見的問題,但是將不勝感激地得到任何幫助。

謝謝。

$result不是字符串,因此無法回顯。 它是從API返回的對象。

您將需要訪問其參數,例如:

echo $result->GetStationBoardResult->locationName

是對您所擁有物品的很好解釋。

嘗試:

echo $result->GetStationBoardResult->trainServices->service->std;
echo $result->GetStationBoardResult->trainServices->service->etd;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM