PHP SoapClient Create An Array of Objects

In PHP, you can’t create an array with the same key more than once. You can’t do this.

$array['transaction'] = "success";
$array['transaction'] = "fail";

Obviously, you can DO that, but “fail” will overwrite “success.”

But when making Soap calls, it’s often necessary to pass in a structure like this.

<users>    
    <user>
    <firstname>Jamal</firstname>
    </user>
    <user>
    <firstname>Acacia</firstname>
    </user>
</users>

But you can’t create an array or an object in PHP that looks like that. Well, you can but it needs to look like this.

$request = (object)array('users'=>(object)array('user' = array(
  (object)array('firstname'=>'Jamal'),
  (object)array('firstname'=>'Acacia')
)));

I think you can do it without casting to objects, but in SOAP, objects more closely map to SOAP structures than arrays or hashes do, in my opinion.

Share Button