我正在尝试访问一个数组,该数组由 Soap 类返回。但我无法迭代它。请帮忙。 $server_输出 = curl_exec ($ch(;
变量 $server_output 具有以下来自 Curl 的返回,
CalculatePremiumStructGeneratePolicyResponse Object
(
[GeneratePolicyResult] => <response><policyno></policyno><totalpremium>0</totalpremium><gc_customerid>100000000000192979</gc_customerid><transactionid>FT10317050001414</transactionid><errortext> Error In Proposal Creation. Error in User Entry:
Maximum Days For Past Policy Should Be 7</errortext></response>
[result:CalculatePremiumWsdlClass:private] =>
[lastError:CalculatePremiumWsdlClass:private] =>
[internArrayToIterate:CalculatePremiumWsdlClass:private] =>
[internArrayToIterateIsArray:CalculatePremiumWsdlClass:private] =>
[internArrayToIterateOffset:CalculatePremiumWsdlClass:private] =>
)
我想将上面的转换为简单的数组
Array
(
[GeneratePolicyResult] => <response><policyno></policyno><totalpremium>0</totalpremium><gc_customerid>100000000000192979</gc_customerid><transactionid>FT10317050001414</transactionid><errortext> Error In Proposal Creation. Error in User Entry:
Maximum Days For Past Policy Should Be 7</errortext></response>
[result] =>
[lastError] =>
[internArrayToIterate] =>
[internArrayToIterateIsArray] =>
[internArrayToIterateOffset] =>
)
请帮忙!
编辑说明:- 当我尝试打印gettype($server_output(时,它说它是一个字符串。
试试
$array = (array) $yourObject;
或者使用这样的东西(仅适用于公共变量(
function object_to_array($data)
{
if (is_array($data) || is_object($data))
{
$result = [];
foreach ($data as $key => $value)
{
$result[$key] = object_to_array($value);
}
return $result;
}
return $data;
}
它可能会对你有所帮助。
$object = CalculatePremiumStructGeneratePolicyResponse Object
(
[GeneratePolicyResult] => <response><policyno></policyno><totalpremium>0</totalpremium><gc_customerid>100000000000192979</gc_customerid><transactionid>FT10317050001414</transactionid><errortext> Error In Proposal Creation. Error in User Entry:
Maximum Days For Past Policy Should Be 7</errortext></response>
[result:CalculatePremiumWsdlClass:private] =>
[lastError:CalculatePremiumWsdlClass:private] =>
[internArrayToIterate:CalculatePremiumWsdlClass:private] =>
[internArrayToIterateIsArray:CalculatePremiumWsdlClass:private] =>
[internArrayToIterateOffset:CalculatePremiumWsdlClass:private] =>
);
$array = json_decode(json_encode($object), True);
经过 Stackflow 上提供的大量尝试和解决方案,没有一个有效,但是当我检查变量的类型时,它说的是字符串。
gettype($server_output( 正在打印字符串。
所以它是一个字符串对象。所以我决定将其作为字符串并尝试解析它。
$server_output = curl_exec ($ch);
$data = explode(" [GeneratePolicyResult] =>", $server_output);
$data = explode("[result:CalculatePremiumWsdlClass:private]", $data[1]);
这样,我过滤了"生成策略结果">