Earlier at home, I recently encountered this problem when I was using Zend_Service_Twitter to get the rateLimitStatus of API calls. One of the values returned by the successful API call contained a dash(-); I needed to do something like this to retrieve the value but it would always display “0″.
echo $twitter_response->reset-time; //note the dash(-) between reset and time ;-) |
…the var_dump() looked like this:
... ['reset-time'] => 'Next year... :P' ... |
Right now, I am at work and decided to re-create the scenario that happened earlier. Here is how I made it work:
<?php $obj = new stdClass(); $prop = 'foo-bar'; $obj->$prop = 'Hello World'; echo $obj->foo-bar; //would echo 0 echo $obj->$prop; //Correct! Echoes "Hello World!" |
I just stored the ‘foo-bar’ string into a variable ($prop) and used $obj->$prop to display the correct values.
If you have anything better, like not using a variable to hold the string value with the dash, please do post it below.
UPDATE:
Pete Shaw, would do it like this:
echo $obj->{'foo-bar'}; |
$obj->{'foo-bar'}
…would be how I’d do it
@Pete Shaw, awesome! I am updating the post. Thanks!