bmwgsa Posted December 19, 2014 Share Posted December 19, 2014 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!! Quote Link to comment https://forums.phpfreaks.com/topic/293186-simplexml-array-problems/ Share on other sites More sharing options...
requinix Posted December 19, 2014 Share Posted December 19, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293186-simplexml-array-problems/#findComment-1500108 Share on other sites More sharing options...
bmwgsa Posted December 20, 2014 Author Share Posted December 20, 2014 That's exactly what the doctor ordered!!!!! Thanks so much!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/293186-simplexml-array-problems/#findComment-1500121 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.