Jump to content

Accessing Object Value that contains whitespace.


xamlit

Recommended Posts

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?

 

 

 

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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.

 

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.