在PHP中访问对象数组并将其转换为简单数组

时间:2018-08-22 11:56:01

标签: php arrays object soap-client

我正在尝试访问一个数组,该数组由Soap类返回。但是我无法对其进行迭代。请帮忙。 $ server_output = curl_exec($ ch);

变量$ server_output从Curl返回以下内容,

CalculatePremiumStructGeneratePolicyResponse Object
    (
        [GeneratePolicyResult] =&gt; <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] =&gt; 
        [lastError:CalculatePremiumWsdlClass:private] =&gt; 
        [internArrayToIterate:CalculatePremiumWsdlClass:private] =&gt; 
        [internArrayToIterateIsArray:CalculatePremiumWsdlClass:private] =&gt; 
        [internArrayToIterateOffset:CalculatePremiumWsdlClass:private] =&gt; 
    )

我想将上面转换为简单数组

 Array
    ( 
        [GeneratePolicyResult] =&gt; <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] =&gt; 
        [lastError] =&gt; 
        [internArrayToIterate] =&gt; 
        [internArrayToIterateIsArray] =&gt; 
        [internArrayToIterateOffset] =&gt; 
    )

请帮助!

编辑说明:-当我尝试打印gettype($ server_output)时,它说是一个字符串。

3 个答案:

答案 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] =&gt; <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] =&gt; 
        [lastError:CalculatePremiumWsdlClass:private] =&gt; 
        [internArrayToIterate:CalculatePremiumWsdlClass:private] =&gt; 
        [internArrayToIterateIsArray:CalculatePremiumWsdlClass:private] =&gt; 
        [internArrayToIterateOffset:CalculatePremiumWsdlClass:private] =&gt; 
    );
$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”