aryan kumar aryan kumar - 1 month ago 6
PHP Question

Single array element becomes object with Zend_Soap_Client

I'm using Zend_Soap_Client and encountering this issue:

<parent>
<child><name>abc</name></child>
<child><name>def</name></child>
</parent>


If there's more than one child element then Zend return array and I can access like

$result->parent->child[0]->name


but if there's only one child node it returns object like:

$result->parent->child->name


Can you please let me know what's wrong with my approach or how can I overcome it?

My sample code:

$client = new Zend_Soap_Client('url', array('wsdl'=>'url));
$result = $client->getResult();


I'm using zend 1.9. The same issue happens with PHP's native SoapClient

Thanks!

Answer

Personally I do not see the need to use Zend_Soap_Client instead of SoapClient because the Zend version does not add anything beneficial, but on the other hand the solution applies to both:

There is an options array parameter in the original SoapClient that accepts plenty of things, and especially this (see http://de3.php.net/manual/en/soapclient.soapclient.php):

The features option is a bitmask of SOAP_SINGLE_ELEMENT_ARRAYS,...

With this option, all array structures in the soap response are not reduced to one single element if they contain only one, but left as is. You are always accessing an array then, which is easier than switching depending on the content.