Jump to content

curly bracket, variable variable

Go to solution Solved by muppet77,

Recommended Posts



I have

$rankseasoncart[0] = 3;

$rankseasoncart[1] = 4;

$rankseasoncart[2] = 5;

and i want to write a piece of code so i can call each one separately by having a variable inside the square brackets.


This doesn't work at all




echo ${"rankseasoncart[".$numbera."]"};


what silly error have I made?


if i do

echo $rankseasoncart[1];

as a test, it works fine!




Edited by muppet77
Link to post
Share on other sites

Variable variables are just that: for variables. Not array values.


Regardless, there's no good reason to use variable variables here. Keep it as the array it is.

Link to post
Share on other sites

If you are wanting to reference specific indexes, then that would seem to me that those indexes have specific meanings. An incrementing integer index should typically be used for an array when the values are a list of the same types of objects (e.g. a list of products in a shopping cart). However, if the items in an array are distinct items that have specific meaning, then I would highly suggest using named indexes. An example could be an array that holds a user's last product, most ordered product, favorite product, etc. Using a numeric based index for those values requires anyone working on the code to "know" what each of those numeric indexes mean. It is far easier and intuitive to use something like this


$rankseasoncart['last_prod'] = 3;
$rankseasoncart['most_prod'] = 4;
$rankseasoncart['favorite_prod'] = 5;
Link to post
Share on other sites
This thread is more than a year old.

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.