How to address a numericly named object member variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Surfr
Forum Newbie
Posts: 2
Joined: Wed Mar 11, 2009 6:31 am

How to address a numericly named object member variable

Post by Surfr »

Using a JSON lib it is creating a resultset which I wish to use, however it has somehow created stdClass variables which have numeric only names and thus are unaddressable by PHP as far as I can tell.

Here's a snippet from a print_r() of the object

Code: Select all

 
            [highlighting] => stdClass Object
                (
                    [000012192] => stdClass Object
                        (
                            [t_text] => Array
                                (
                                    [0] => 
                [advert]
                PRIORY PRESS LIMITED
THE FRIARY - <em>CARDIFF</em>
 
TELEPHONE 3578
              
                                )
 
                        )
 
                    [000012193] => stdClass Object
                        (
                            [t_text] => Array
                                (
                                    [0] =>  Religion.
Low Performing Fees
THE BOOK - 1/6
From the Publishers?
PRIORY PRESS LIMITED
The Friary - <em>Cardiff</em>
              
                                )
 
                        )
 
if I try this

Code: Select all

 
echo $this->results->highlighting->000012192->t_text[0];
 
I get this error

Code: Select all

 
[Wed Mar 11 11:32:57 2009] [error] [client xx.xx.xx.xx] PHP Parse error:  syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$' in /var/www/ccymod/application/views/scripts/browse/search.phtml on line 62
 
I'm stumped. Any ideas?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How to address a numericly named object member variable

Post by VladSun »

Code: Select all

echo $this->results->highlighting->{'000012192'}->t_text[0];
There are 10 types of people in this world, those who understand binary and those who don't
Surfr
Forum Newbie
Posts: 2
Joined: Wed Mar 11, 2009 6:31 am

Re: How to address a numericly named object member variable

Post by Surfr »

VladSun I owe you a $drink

Many thanks. I'd never seen that notation for vars before. Problem solved :D
Post Reply