0

DETAILS

I have a separate problem that I am investigating that seems to be related to the way I am accessing arrays. Hence this possibly odd question.

I have the following array

$response['custom_validation']['agreetotos0'] ='zero';
$response['custom_validation']['agreetotos1'] ='one';

I would like to use the current subscription level to determine the agreetotos name. For the moment let's assume that $subscriptionlevel =1;

That means the value I am trying to retrieve = $response['custom_validation']['agreetotos1'];

I know I can access this value by using $response['custom_validation']['agreetotos'.$subscriptionlevel];

or I can use variable variables to access the array with the following

$response['custom_validation']['agreetotos'.${'subscriptionlevel'}];

QUESTION

Are there any other ways?

If yes, what are the advantages/drawbacks of using them?

EDIT

I haven't properly explained what I am trying to achieve. I'm looking for syntax equivalent to $response['custom_validation']['agreetotos1']

For example, $response['custom_validation']['agreetotos'][1] is not equal to $response['custom_validation']['agreetotos1']

whereas

$response['custom_validation']['agreetotos'.$subscriptionlevel] is the same as $response['custom_validation']['agreetotos1'].

Sorry for any confusion.

TryHarder
  • 2,704
  • 8
  • 47
  • 65

1 Answers1

2

One simple way to do this is..

$response['custom_validation']['agreetotos'][0] ='zero';
$response['custom_validation']['agreetotos'][1] ='one';

And you can access this as..

$response['custom_validation']['agreetotos'][$subscriptionlevel];
Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59
  • I can see the merit in writing it this way even though it is not quite what I meant. At first, I thought you were saying that `$response['custom_validation']['agreetotos'][1]` was the same as `$response['custom_validation']['agreetotos1']` – TryHarder Jun 04 '13 at 06:50