Jump to content

Displaying associated array element?

Recommended Posts


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',


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 .






Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

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

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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