Jump to content

Simplexml array problems


bmwgsa

Recommended Posts

Hi All,

 

I'm new to PHP (not to coding) - and I've been able to bang out most answers using books, the web and brue force - but this one has me stumped.

(If it's been covered before, sorry, I'm being lazy for not searching)...

 

So, here's the problem:

 

I have a call to my internal system that returns XML data via a SOAP call.

The call works great - data is returned as I would expect.

 

Like I've read, I put said data into a variable usng simpleXML like this: $xml = simplexml_load_string($response3); ($response3 being where the raw data is)

 

When I do a var_dump of $xml, I get this:

 

SimpleXMLElement Object
(
    [iCProductAvailByWhseResult] => SimpleXMLElement Object
        (
            [errorMessage] => SimpleXMLElement Object
                (
                )

            [arrayAvailability] => SimpleXMLElement Object
                (
                    [iCProductAvailByWhse.output.Availability] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [productCode] => xxx       
                                    [description1] => yyy          
                                    [description2] => zzz
                                    [warehouse] => aaa
                                    [netAvailable] => bbb
                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [productCode] => xxx
                                    [description1] => yyy          
                                    [description2] => zzz
                                    [warehouse] => aaa
                                    [netAvailable] => bbb
                                )

                            [2] => SimpleXMLElement Object
                                (
                                    [productCode] => xxx
                                    [description1] => yyy          
                                    [description2] => zzz
                                    [warehouse] => aaa
                                    [netAvailable] => bbb
                                )
 

**** Edited, I got real data, not xxx,yyy, etc... ****

 

Now the issue:  It seems like I have everything in place - all I need now is to get my data off to variables for display.

I've tried everything I can think of and I just can't seem to get it.

 

I ran $xml->getName() and it gave me: ICProductAvailByWhseResult

 

So, simple I think, I should be able to print something like $xml->ICProductAvailByWhseResult[0]->warehouse and I should get something, right?

Nope.

 

So I try $xml->ICProductAvailByWhseResult[1]->warehouse and I get a notice that I'm trying to get to a property of a non-object.

 

What am I missing?  I come from a VB backgound, so I understand array's and such - but this one has be stumped.

 

And help would be greatly appreciated...

 

Thanks!!

 

Link to comment
Share on other sites

Like with a print_r() or var_dump() of an array, you have to navigate down through the layers to get the data you want.

 

You're starting at the top. First (and only) thing you can use is ICProductAvailByWhseResult. Your next choice is between errorMessage and arrayAvailability, of which you want the latter. Then "ICProductAvailByWhse.output.Availability" is next, but mere -> syntax isn't enough to access that. Then you'll get an array, and then you can do [0]->warehouse.

foreach ($xml->ICProductAvailByWhseResult->arrayAvailability->{"ICProductAvailByWhse.output.Availability"} as $availability) {
    echo (string)$availability->warehouse;
Note the (string) because pretty much everything you get from a SimpleXMLElement object will be another SimpleXMLElement object - cast to string to get the plain value. Technically it's unnecessary here because echo will do it automatically for you, but do it anyway so you get in the habit.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.