1

I searched, but the other threads don't answer my question, or I am just too stupid to see it.

$Questions = array(
1 => array(
    'Question' => 'Second question',
    'Answers' => array(
        'A' => 'First answer of Second question',
        'B' => 'Second answer Second question',
        'C' => 'Third answer Second question'
    ),
    'CorrectAnswer' => 'C'
)
);

How do I echo the value of 'CorrectAnswer'? Actually, I mean the value of 'C' (In this case, it would be "Third answer Second question"). $Value['CorrectAnswer'] gives me "C". But I can't just echo 'C', because in each array the correct answer is different. I am sure it's something really simple, but it escapes me. Sorry if I am not explaining it properly. This is the other piece of code:

foreach ($Questions as $QuestionNo => $Value)
{
    if ($Answers[$QuestionNo] != $Value['CorrectAnswer'])
{
        echo 'Your answer: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br />
Correct answer: <span style="color: green;">'.$Value['CorrectAnswer'].'</span>';                        
    } 
}
3
  • 1
    It's a bad practice to name your variables with first letter capitalized. Usually class names get first letter capital. But still, that's your choice :) Commented Jan 30, 2015 at 15:19
  • @Whirlwind: yeah, should be camelCase, right? Commented Jan 30, 2015 at 15:29
  • Exactly...Or you can use underscores. Commented Jan 30, 2015 at 15:32

4 Answers 4

1

You would use the value for CorrectAnswer as lookup key against Answers:

echo $Questions[1]['Answers'][$Questions[1]['CorrectAnswer']];

Or in context of your code example, which is looping through $Questions array:

foreach ($Questions as $questionNo => $value) {
    $answerGiven = $answers[$questionNo];
    $correctAnswer = $value['Answers'][$value['CorrectAnswer']];
    if ($answerGiven !== $correctAnswer) {
        echo 'Your answer: <span style="color: red;">' . $answerGiven . '</span>';
        echo '<br />';
        echo 'Correct answer: <span style="color: green;">' . $correctAnswer . '</span>';
    } 
}

Note that I have cleaned up the code to make it much more readable. You set the answer given and correct answer to variables and then use these for decision making and output. I also indented code properly to make it more readable.

I would caution you against using initial caps for variable naming (or associative array key naming) in PHP as this is VERY non-standard.

Sign up to request clarification or add additional context in comments.

Comments

1

The value of $Value['CorrectAnswer'] would be "C", so you can use it like so:

echo $Value['Answers'][$Value['CorrectAnswer']];

2 Comments

Thank you, that's it. So what it does is use the value of 'CorrectAnswer' as the position of the array 'Answers' which to echo?
@KardNails Yes, it would be as if you said: $Value['Answers']['C'];
1

Are you looking for something like

echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';

Comments

1

Are you asking for this:

$Value['Answers'][$Value['CorrectAnswer']]

This should give you: 'Third answer Second question'

(well, only in the case where $Value references the 1st array in the $Questions array, that is.)

4 Comments

It should be $Value, not $value. Also didn't I already give this as an answer?
Well, that's the asynchronous internet for you! We answered at the same time. And you're correct, it should be $Value. I'll fix it.
Actually, about the async stuff. You're posted at about 6 minutes before me, so yes, it is repeat. However, I didn't see your post before I posted, due to refresh, I'm sure. Else I wouldn't have posted a repeat answer.
It's fine, I know you didn't copy me. I sometimes spend some time on an answer to just find out it was already posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.