访问 PHP 中的对象数组并将其转换为简单数组
Accesing a Object array in PHP and convert it to Simple Array
我正在尝试访问一个数组,它是 Soap class 的 return。但我无法迭代它。请帮忙。
$server_output = curl_exec ($ch);
变量 $server_output 有以下来自 Curl 的 return,
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) 时,它说它是一个字符串。
可能对你有帮助。
$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);
试试
$array = (array) $yourObject;
或使用类似的东西(仅适用于 public 变量)
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;
}
在 Stackflow 上进行了大量尝试和解决方案后,none 有效,但是当我检查变量类型时,它说的是字符串。
gettype($server_output) 正在打印字符串。
所以它是一个字符串对象。所以我决定把它当作字符串并尝试解析它。
$server_output = curl_exec ($ch);
$data = explode(" [GeneratePolicyResult] =>", $server_output);
$data = explode("[result:CalculatePremiumWsdlClass:private]", $data[1]);
通过这种方式,我过滤了 "GeneratePolicyResult"
我正在尝试访问一个数组,它是 Soap class 的 return。但我无法迭代它。请帮忙。 $server_output = curl_exec ($ch);
变量 $server_output 有以下来自 Curl 的 return,
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) 时,它说它是一个字符串。
可能对你有帮助。
$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);
试试
$array = (array) $yourObject;
或使用类似的东西(仅适用于 public 变量)
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;
}
在 Stackflow 上进行了大量尝试和解决方案后,none 有效,但是当我检查变量类型时,它说的是字符串。
gettype($server_output) 正在打印字符串。
所以它是一个字符串对象。所以我决定把它当作字符串并尝试解析它。
$server_output = curl_exec ($ch);
$data = explode(" [GeneratePolicyResult] =>", $server_output);
$data = explode("[result:CalculatePremiumWsdlClass:private]", $data[1]);
通过这种方式,我过滤了 "GeneratePolicyResult"