0

I am trying to accessing a array, which is return by Soap class. But I am not able to iterate it. Please help. $server_output = curl_exec ($ch);

Variable $server_output has following return from 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; 
    )

I want to convert above into simple array

 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; 
    )

Please help!

Edit Note :- When I try to print gettype($server_output), it says its a string.

7
  • you cannot access private property of object array. So you need to change initial-one to get data as public property Commented Aug 22, 2018 at 12:12
  • GeneratePolicyResult is not private. At least we can access it. @Alive to Die' Commented Aug 22, 2018 at 12:17
  • try echo $objArray->GeneratePolicyResult; Commented Aug 22, 2018 at 12:21
  • Tried, $server_output = curl_exec ($ch); echo $server_output->CalculatePremiumStructGeneratePolicyResponse->GeneratePolicyResult;, getting error Trying to get property of non-object Commented Aug 22, 2018 at 12:24
  • try this echo $server_output->GeneratePolicyResult; Commented Aug 22, 2018 at 12:26

3 Answers 3

1

just try

$array =  (array) $yourObject;

or use something like this(for public variables only)

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

This is unlikely to be a viable solution in most use cases, as simply casting the object to an array can mess with the keys
@WilliamPerron you can write some function
The proposed function still has the same issue where it might mess with key names, especially for private properties
@WilliamPerron with this function you lose all private and protacted variables for sure... first method change their names
@WilliamPerron but i think if it private, you have not to get them at all...only with special functions
|
0

it might help you.

$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);

4 Comments

Tried. $server_output = curl_exec ($ch); $server_output =$object; $array = json_decode(json_encode($object), True); print_r($array);
Its still same. No diff. prinitng same
$server_output = curl_exec ($ch); $array = json_decode(json_encode($server_output), True);
Edit Note :- When I try to print gettype($server_output), it says its a string.
0

After a lot of try and solutions available on Stackflow, none of works, But When i check the type of variable, it was saying string.

gettype($server_output) was printing string.

So It was a string object. SoI decided to take it as string and try to parse it.

$server_output = curl_exec ($ch);  
$data = explode(" [GeneratePolicyResult] =>", $server_output); 
$data = explode("[result:CalculatePremiumWsdlClass:private]", $data[1]); 

And in this way,I have filter the "GeneratePolicyResult"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.