David-London Posted July 13, 2019 Share Posted July 13, 2019 Hi, I have this array: $cats = array( "A"=>'Business Expansion Specialist', "B"=>'Affiliate Page', "C"=>'Personal: Hobbies', "D"=>'Personal: Pets', "E"=>'Personal: Holidays', "F"=>'Personal: Sport', "G"=>'Personal: C.V.', "H"=>'Personal: Other', "I"=>'For Sale: Antiques', "Z"=>'Other' ); And then from a table I get the variable $cat_cd. $cat_cd contains 'F' so it refers to 'Personal: Sport' If I want to put that in $category, how would I write that? I thought it might be .... $category = $cats['$cat_cd']; I tried that and it didn't work . Thanks Quote Link to comment https://forums.phpfreaks.com/topic/308956-displaying-associated-array-element/ Share on other sites More sharing options...
ginerjm Posted July 13, 2019 Share Posted July 13, 2019 A rule of PHP is that php vars do not get interpreted if wrapped in single quotes. Leave them off in this case or use double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/308956-displaying-associated-array-element/#findComment-1568348 Share on other sites More sharing options...
requinix Posted July 13, 2019 Share Posted July 13, 2019 To add to that, only put variables into (double-quoted) strings if you want a string and it's going to be more than just the contents of that variable. Like writing "$foo" is making it unnecessarily complicated - just $foo is enough. Meanwhile "abc $foo xyz" makes sense because you've got the "abc" and "xyz" bits in there too. unless you want to cast the variable to a string, which "$foo" would do and $foo would not, but if that's the case then you should cast it like (string)$foo instead Quote Link to comment https://forums.phpfreaks.com/topic/308956-displaying-associated-array-element/#findComment-1568349 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.