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?

 

 

 

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;

 

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.