L
L
lehasta2017-03-18 03:09:19
PHP
lehasta, 2017-03-18 03:09:19

How to refer to an element in stdClass if the element contains a dash?

print_r ($AUT); //Получаю это
stdClass Object ( 
        [user-badges] => Array ( [0] => stdClass Object ( [name] => had_featured_item [label] => Featured Item [image] => https://public-assets.envato-static.com/assets/badges/had_featured_item-7e2ccab79c4585f65dfd61918aad8ff00cde789a34ec84e4fc8cf5b3fe511a2e.svg ) 
                     [1] => stdClass Object ( [name] => had_trending_item [label] => Trendsetter [image] => https://public-assets.envato-static.com/assets/badges/had_trending_item-b7a0c2c3954e4704f760d9ac81594f71cef9da4f00a873ce6ab5e000af1b1d6a.svg ) 
                     [2] => stdClass Object ( [name] => country_pk [label] => Pakistan [image] => https://public-assets.envato-static.com/assets/badges/country_pk-562c21eee296804de5224fc84c4c4a5af270a1b5b51565d3297ebb320d591d6a.svg ) 
                     [3] => stdClass Object ( [name] => most_wanted_bounty_winner [label] => Most Wanted Winner [image] => https://public-assets.envato-static.com/assets/badges/most_wanted_bounty_winner-1482d585996f8966df1853da1b51509b6397013d34e7522e61ff039f85d5055d.svg ) 
                     [4] => stdClass Object ( [name] => author_level_6 [label] => Author Level 6 [image] => https://public-assets.envato-static.com/assets/badges/author_level_6-92e108009069ae7fb9a29106deb4300b6cdcde236d5d7688edcc2bf1b4faf4b4.svg ) 
                     [5] => stdClass Object ( [name] => exclusive [label] => Exclusive Author [image] => https://public-assets.envato-static.com/assets/badges/exclusive-c46b38381f19512d29603bebe1a1fae3a44a4147dddef99185ee228667c17a66.svg ) 
                     [6] => stdClass Object ( [name] => veteran_level_3 [label] => 3 Years of Membership [image] => https://public-assets.envato-static.com/assets/badges/veteran_level_3-a7e234054f28f9cca7ee8a3a5692619f40133fddbd8b8b73efdd94d3dde78514.svg ) 
                     [7] => stdClass Object ( [name] => collector_level_2 [label] => Collector Level 2 [image] => https://public-assets.envato-static.com/assets/badges/collector_level_2-3c965fe571f6b45ce222862ed5ee1312a9dca9993a7ae7a8f22ce17a2b42cf4d.svg ) 

///////////////////////////////////////////////////////////////////////////// А так 
echo  $AUT->user-badges->name[0];
// получаю это - Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';' in W:\domains\  и т.д.
// понимаю что "user-badges" - не правильно. Подскажите пожалуйста как правильно?

                     )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2017-03-18
@lehasta

ideone.com/49754N

A
Andrey Pavlenko, 2017-03-18
@Akdmeh

Should also work shorter than $AUT->{'user-badges'}[0]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question