【问题标题】:Help figuring out nested arrays from SOAP response?帮助找出 SOAP 响应中的嵌套数组?
【发布时间】:2011-11-01 09:00:17
【问题描述】:

我正在使用 PHP 为海洋潮汐编写一个小 Web 应用程序。我在弄清楚如何访问返回的数组(PHP 转换为 stdObject)时遇到问题。

WSDL 文件位于:http://opendap.co-ops.nos.noaa.gov/axis/webservices/highlowtidepred/wsdl/HighLowTidePred.wsdl

我的 PHP 代码是:

    $wsdl = "http://opendap.co-ops.nos.noaa.gov/axis/webservices/highlowtidepred/wsdl/HighLowTidePred.wsdl";

    $tides = new soapclient($wsdl);

    $tideParams = array(  
        'stationId' => '8454000',
        'beginDate' => '20110821 00:00',
        'endDate' => '20110821 23:59',
        'datum' => '0',
        'unit' => '0',
        'timeZone' => '0'
    );

    $tideRet = $tides->getHighLowTidePredictions($tideParams);
    var_dump($tideRet);

这个转储返回:

   object(stdClass)#2 (1) {
      ["HighLowValues"]=>
      object(stdClass)#3 (1) {
        ["item"]=>
        object(stdClass)#4 (2) {
          ["data"]=>
          array(4) {
            [0]=>
            object(stdClass)#5 (3) {
              ["time"]=>
              string(5) "00:35"
              ["pred"]=>
              float(3.8)
              ["type"]=>
              string(1) "H"
            }
            [1]=>
            object(stdClass)#6 (3) {
              ["time"]=>
              string(5) "05:45"
              ["pred"]=>
              float(0.7)
              ["type"]=>
              string(1) "L"
            }
            [2]=>
            object(stdClass)#7 (3) {
              ["time"]=>
              string(5) "12:49"
              ["pred"]=>
              float(4.2)
              ["type"]=>
              string(1) "H"
            }
            [3]=>
             object(stdClass)#8 (3) {
              ["time"]=>
              string(5) "18:32"
              ["pred"]=>
              float(1.3)
              ["type"]=>
              string(1) "L"
            }
          }
          ["date"]=>
          string(10) "08/21/2011"
        }
      }
    }

我不知道如何阅读这个,我的谷歌搜索也没有多大帮助。任何帮助或指导表示赞赏。

【问题讨论】:

  • 当您在页面上查看源代码时,您能否重新发布var_dump() 的显示方式(带有换行符和缩进)?在这样的块中阅读太难了
  • 我没有意识到转储在源代码中看起来不同。我看着那团乱七八糟的东西,试图把它分开。哈哈。不过仍然不知道如何将其分离为可用的数组。

标签: php soap noaa


【解决方案1】:

这是一个动态的 PHP 对象。所有引用的项目都是属性名称,因此要获取数据数组:

$data = $tides->getHighLowTidePredictions($tideParams)
              ->HighLowValues
              ->item
              ->data;

然后,如果您想获取特定项目的时间属性,例如,您将寻址该数组索引并查找时间属性:

 $data[0]->time;

【讨论】:

  • 很好的答案!通过一个简单的 foreach,我现在可以轻松访问所有数据。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2019-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 2020-08-12
相关资源
最近更新 更多