PHP variable variable inside []

Thanks for the help below, however, I still have some problems, I will try to explain further:

I have a variable like this:

$value['questions'];

      

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

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

      

but the problem I am facing is that the value $value['questions']

is q1, but I need 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 looks like this:

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

      

Make sense?

Thanks again for your help.

Hi everyone,

I hope there is an easy solution!

I have a variable:

$results['q1'];

      

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

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

      

Couldn't find a solution on google and research PHP manuals ...

Anyone got a suggestion / solution?

Thanks,

Homer.

+2


a source to share


4 answers


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

As an example, consider this piece 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 fairly simple variable, but you could use whatever you want between [

and ]

, for example:

  • functions: $results[ my_function($parameter) ]

  • array element: $result[ $my_array['indice'] ]

    • This is most likely what you want to do.
  • object property: $result[ $obj->data ]

  • ...

Basically, you can use almost anything you want - as long as it evaluates to a scalar value (i.e. one integer, string)


In your specific case, you could declare a bit $results

like this:

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

      

And it $i

will be declared like this:

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

      

This means what $i['question']

will be 'q1'

and what the next part of the code is:

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

      

You will get this result:

first question

      





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

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

      

Here:

  • $indice

    'variable'

  • and $$indice

    -'a'

  • which means you will get the same result as before.


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

Why is the paragraph $ foo [bar] wrong? might be of interest, especially given the example you posted.





Edit after OP edit:

If $value['questions']

- 'q1'

, then the following two pieces of code:

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

      

and

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

      

should do the same: the from $results[$value['questions']]

part $value['questions']

will be evaluated (before 'q1'

) to the rest of the expression and will be the same as $results['q1']

.


The following piece of code is shown as an example:

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

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

      

Outputs 4

.

+6


a source


You can do:

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

      

It will first access the array $i

to get the value corresponding to the key 'question'

... This value will be used as a key in 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 a single quote, the variable interpolation has no , and the entire string is literally processed.



Alternatively, you can also:

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

      

+4


a source


<?php

$result['q1'];

// is equivalent to

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

      

Now $index

can be as complex as you want. For example, something like this is possible:

$result[ 'q' . round( $someArray[$i] / 2 ) ];

      

0


a source


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

      

0


a source







All Articles