我正在尝试访问一个数组,该数组由Soap类返回。但是我无法对其进行迭代。请帮忙。 $ server_output = 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)时,它说是一个字符串。
答案 0 :(得分:1)
尝试
$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;
}
答案 1 :(得分:0)
它可能会对您有所帮助。
$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);
答案 2 :(得分:0)
在Stackflow上进行了大量尝试和解决方案后,这些方法都无效,但是当我检查变量的类型时,它说的是字符串。
gettype($ server_output)正在打印字符串。
所以这是一个字符串对象。因此,我决定将其作为字符串并尝试对其进行解析。
$server_output = curl_exec ($ch);
$data = explode(" [GeneratePolicyResult] =>", $server_output);
$data = explode("[result:CalculatePremiumWsdlClass:private]", $data[1]);
通过这种方式,我过滤了“ GeneratePolicyResult”