Jump to content

curly bracket, variable variable


muppet77

Recommended Posts

Hi,

 

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

 

$numbera=1;

 

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

 

what silly error have I made?

 

if i do

echo $rankseasoncart[1];

as a test, it works fine!

 

 

thanks.

Link to comment
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 comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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