我目前正在使用以下方法从Web服务检索信息:
$serviceData= new SoapClient('http://xxx.xxx.xxx.xx:xx/WebService/WebService.svc?wsdl');
$response = $serviceData->GetMyInformation();
var_dump($response);
var_dump的结果如下:
object(stdClass)#15 (1)
{ ["GetDatabaseResult"]=> object(stdClass)#16 (1)
{ ["DatabaseInformation"]=> array(4)
{ [0]=> object(stdClass)#17 (2)
{ ["DateCreated"]=> string(19) "2016-07-06T09:36:03" ["CurrencyCode"]=> string(3) "USD" }
[1]=> object(stdClass)#18 (2)
{ ["DateCreated"]=> string(19) "2016-12-07T02:49:02" ["CurrencyCode"]=> string(3) "USD" }
[2]=> object(stdClass)#19 (2)
{ ["DateCreated"]=> string(19) "2016-12-07T02:52:38" ["CurrencyCode"]=> string(3) "USD" }
[3]=> object(stdClass)#20 (2)
{ ["DateCreated"]=> string(19) "2016-12-07T02:53:38" ["CurrencyCode"]=> string(3) "USD" }
}
}
}
我需要的是一个foreach循环,我可以检索每个键和值:
DateCreated: 2016-07-06T09:36:03
CurrencyCode: USD
我尝试使用json_encode($response)
删除了object(stdClass)#15 (1)
和json_dencode($response)
,这样就达到了这一点:
array(1)
{ ["GetDatabaseResult"]=> array(1)
{ ["DatabaseInformation"]=> array(4)
{ [0]=> array(2)
{ ["DateCreated"]=> string(19) "2016-07-06T09:36:03" ["CurrencyCode"]=> string(3) "USD" }
[1]=> array(17)
{ ["DateCreated"]=> string(19) "2016-12-07T02:49:02" ["CurrencyCode"]=> string(3) "USD" }
[2]=> array(17)
{ ["DateCreated"]=> string(19) "2016-12-07T02:52:38" ["CurrencyCode"]=> string(3) "USD" }
[3]=> array(17)
{ ["DateCreated"]=> string(19) "2016-12-07T02:52:38" ["CurrencyCode"]=> string(3) "USD" }
}
}
}
我知道它是一个嵌套数组,但是我该如何解析呢?
答案 0 :(得分:1)
无需编码/解码循环。试试这个:
foreach($response->GetDatabaseResult->DatabaseInformation as $entry){
error_log("Date Created: ".$entry->DateCreated."; Currency Code: ".$entry->CurrencyCode);
}