tags:

views:

90

answers:

4

Thanks for the help below, however, I'm still having some problems, I'll try and explain further:

I have a variable as follows:

$value['questions'];

Now, I need to do a check in a loop, so I have this piece of code in the loop:

if($results[$value['questions']]==4){blah blah blah};

but the problem I am having is that the value of $value['questions'] is q1 but I need the q1 to be a string (i.e. inside quotes '') in the $results section of the code, so the $results element should look like this...

if($results['q1']==4){blah blah blah};

currently it looks like this

if($results[q1]==4){blah blah blah};

Make sense?

Thanks again for the help.

Hi all,

I hope there is a simple solution!

I have a variable:

$results['q1'];

Is it possible to have the 'q1' element as a variable, something like this:

$results['$i[question]'];

Have not been able to find a solution on Google and researching the PHP manuals...

Anyone got a suggestion/ solution?

Thanks,

Homer.

+4  A: 

You can do:

$results[$i['question']];

This will first access the array $i to get the value corresponding to the key 'question'. That value will be used as the key into the array $results.

Your way: $results['$i[question]']; is actually trying to get the value corresponding to the key '$i[question]' in the array $results. Since the key is in single quote, variable interpolation does not happen and the entire string is treated literally.

Alternatively you can also do:

   $results["$i[question]"]; // using double quotes also works.

Example:

$arr1 = array('php' => 'zend', 'java' => 'oracle');
$arr2 = array('p' => 'php', 'j' => 'java');

echo $arr1[$arr2['p']]; // prints zend.
echo $arr1["$arr2[p]"]; // prints zend.
codaddict
@Uni - thanks - I am trying to bring back the value corresponding to the key, as you state in your first para - I need to use this as a string though - see updated question.
Homer_J
@Hormer: It will be treated as the string by default. You need to do any additional thing.
codaddict
A: 
<?php

$result['q1'];

// is equivalent to

$index = 'q1';
$result[ $index ];

Now $index can be as complex as you want it to be. For example something like this is also possible:

$result[ 'q' . round( $someArray[$i] / 2 ) ];
poke
OFC that this index (something like (q1 OR q0) must exist...
TiuTalk
But that applies to every array access..
poke
+5  A: 

Yes, it is possible to use a variable as indice, when accessing an array element.

As an example, consider this portion of code :

$results = array(
    'a' => 'sentence a', 
    'b' => 'hello !', 
);

$indice = 'a';
echo $results[$indice];

Which will give the following output :

sentence a


Here, $indice is a quite simple variable, but you could have used whatever you wanted between the [ and ], like, for instance :

  • functions : $results[ my_function($parameter) ]
  • array element : $result[ $my_array['indice'] ]
    • Which seems to be what you want to do ?
  • object property : $result[ $obj->data ]
  • ...

Basically, you can use pretty much whatever you want there -- as long as it evaluates to a scalar value (i.e. a single integer, string)


In your specific case, you could have $results declared a bit like this :

$results = array(
    'q1' => 'first question', 
    'q2' => 'second question', 
);

And $i would be declared this way :

$i = array(
    'question' => 'q1'
);

Which means that $i['question'] would be 'q1', and that the following portion of code :

echo $results[ $i['question'] ];

Would get you this output :

first question



Edit : To answer the specific words you used in the title of your question, you could also use what's called variable variable in PHP :

$variable = 'a';
$indice = 'variable';
echo $results[ $$indice ];

Here :

  • $indice is 'variable'
  • and $$indice is 'a'
  • which means you'll get the same output as before


And, of course, don't forget to read the Arrays section of the PHP manual.

The Why is $foo[bar] wrong? paragraph could be of interest, especially, considering the example you firt posted.



Edit after the edit of the OP :

If $value['questions'] is 'q1', then, the two following portions of code :

if($results[$value['questions']]==4){blah blah blah}

and

if($results['q1']==4){blah blah blah}

should do exactly the same thing : with $results[$value['questions']], the $value['questions'] part will be evaluated (to 'q1') before the rest of the expression, and that one will be the same as $results['q1'].


As an example, the following portion of code :

$results = array(
    'q1' => 4, 
    'q2' => 6, 
);
$value = array('questions' => 'q1');

if($results[$value['questions']]==4) {
    echo "4";
}

Outputs 4.

Pascal MARTIN
@Pascal - thanks for the heads up - don't know if you can then answer the question above?
Homer_J
@Homer : I've edited my answer with an example that's closer to your specific situation ; hope this helps :-)
Pascal MARTIN
@Pascal - nearly answered but not quite - the value of $value['questions' is q1 NOT 'q1' but the $results section of the code currently shows $results[q1] and NOT $results['q1'] and therefore won't work. Any further help. Thanks in advance.
Homer_J
if the value of `$value['questions']` is `q1`, then is it a string *(no other PHP type can contain this)* ;;; `'q1'` is just the way people write it, to indicate it's a string, as PHP expects strings to be enclosed in quotes, when written in PHP code. ;;;; if `$a` contains the value `q1`, then `$results[$a]` will be exactly the same as `$results['q1']`.
Pascal MARTIN
Ok, thanks for the clarification - then it would seen my check function maybe flawed :(
Homer_J
You're welcome :-) ;; good luck !
Pascal MARTIN
A: 

CODE

<?php

$results = array(
    'q1' => 'This is a result called q1.',
    'q2' => 'So, can you access this?',
    '$i[question]' => 'This is a result called $i[question]',
);

$i = array(
    'question' => 'q2',
    'answer'   => 'Yes.'
);

echo '<pre>';
echo $results['q1'] . "\n";              // Using Array's is easy -
echo $results['$i[question]'] . "\n";    // all you gotta do is make sure
echo $results[$i['question']] . "\n";    // that you understand how
echo $i['answer'];                       // the keys work.

?>

OUTPUT

This is a result called q1.
This is a result called $i[question]
So, can you access this?
Yes.
Allyn
Thanks Allyn, I'm getting there but slowly, I've updated my questions, any further suggestions?
Homer_J