This is a follow up to my post about array problems with SOAP and PHP. The scenario was that when an array with only one object was returned over SOAP the array was discarded and pointed straight to the single object.

Cmm was kind enough to tip me this PHP bug report, which explains that this is actually not a bug, but a feature.

To get arrays containing one single element, simply create the SoapClient as follows:

1
$x = new SoapClient($wsdl, array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));

Related articles:

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • DZone
  • Live
  • Slashdot

8 Responses to “Array problems with SOAP and PHP - Updated”

  1. Balu Says:

    *Gna*, this post a few days ago would’ve really helped me :)
    I tried to work around that problem by

    if (!is_array($something)) $something = array($something);

    I think I better check the options next time…

  2. eirikhoem Says:

    Yeah, the code I’m running now has quite a few hacks like that. Feels good to be able to clean it up :)

  3. James Morris Says:

    This is a usefull GOTCHA that i’ll be implementing soon.

  4. Lukas Says:

    Not sure if that bug is finally fixed, but the mode you mention used to turn NULL into array(NULL), which is also not what you really expect imho.

  5. Eirik Hoem’s Blog: Array problems with SOAP and PHP - Updated | Development Blog With Code Updates : Developercast.com Says:

    […] Hoem has posted an update on a previous problem he was having when working with SOAP in PHP and its handling of arrays. The […]

  6. Matt Says:

    Wow, wish i would have known this 6 months ago. Did all sorts of hacks to get around that. My question is… where is this documented?

  7. Matt Says:

    By the way when using soap has anyone seen php segfaults when the other end doesn’t respond?

  8. Eirik Hoem’s Blog » Blog Archive » Setting xsi:type for objects sent over SOAP (inheritance) Says:

    […] Array problems with SOAP and PHP - Updated Category: PHP, SOAP, Tips, X-Fire […]

Leave a Reply