xamlit Posted March 8, 2011 Share Posted March 8, 2011 Hello All: I am trying to access a value within an object. The value name has whitespace in it and is called "zip code". When I try to access the value via regular object call such as: $sub = $sub->zip code; PHP goes crazy on me. I've tried to encapsulate the value name within a variable like this: $zip_code = "Zip Code"; $sub = $sub->zip_code; and that doesn't work either. Does anyone have any ideas about how I should go about yanking the data out? Quote Link to comment https://forums.phpfreaks.com/topic/230015-accessing-object-value-that-contains-whitespace/ Share on other sites More sharing options...
HuggieBear Posted March 8, 2011 Share Posted March 8, 2011 Have you tried curly braces? $sub = $sub->{zip code} This doesn't seem right, do you have the code for us to look at? Quote Link to comment https://forums.phpfreaks.com/topic/230015-accessing-object-value-that-contains-whitespace/#findComment-1184664 Share on other sites More sharing options...
PFMaBiSmAd Posted March 8, 2011 Share Posted March 8, 2011 You will probably need some single-quotes as well - Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe. Example #3 Getting <line> <?php include 'example.php'; $xml = new SimpleXMLElement($xmlstr); echo $xml->movie->{'great-lines'}->line; Quote Link to comment https://forums.phpfreaks.com/topic/230015-accessing-object-value-that-contains-whitespace/#findComment-1184668 Share on other sites More sharing options...
xamlit Posted March 8, 2011 Author Share Posted March 8, 2011 Thanks to a great coworker I solved the problem. I referenced the value that had white space by using the following code: $sub = $sub["Zip Code"]; Note the double quotes and square brackets. Thank you all who have taken the time to help me. Quote Link to comment https://forums.phpfreaks.com/topic/230015-accessing-object-value-that-contains-whitespace/#findComment-1184679 Share on other sites More sharing options...
PFMaBiSmAd Posted March 9, 2011 Share Posted March 9, 2011 That looks like it is for accessing an attribute, rather than a data value. Quote Link to comment https://forums.phpfreaks.com/topic/230015-accessing-object-value-that-contains-whitespace/#findComment-1184937 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.