Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 761245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:48:53+00:00 2026-05-14T15:48:53+00:00

Thanks for the help below, however, I’m still having some problems, I’ll try and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:48:54+00:00Added an answer on May 14, 2026 at 3:48 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.