kingnutter Posted March 16, 2010 Share Posted March 16, 2010 Hi everyone, I am trying to generate questions for a simple captcha system on a contact page, but I'm having trouble extracting a single entry from an associative array in the form $question, $answer. This is the array: $question_answers = array ( "Is The Moon red or white?" => 'white', "Is rain wet or dry?" => 'wet', "Is The Sun blue or yellow?" => 'yellow', "Is fire hot or cold?" => 'hot', "Was Einstein clever or stupid?" => 'clever', "Is ice cream hot or cold?" => 'cold' ); I've tried generating a random number to extract $question_answers[$randomnumber] but the variable is always empty. Should I be using a multidimensional array? Link to comment https://forums.phpfreaks.com/topic/195492-generating-captcha-questions/ Share on other sites More sharing options...
fr34k Posted March 16, 2010 Share Posted March 16, 2010 $question_answers = array ( "Is The Moon red or white?" => 'white', "Is rain wet or dry?" => 'wet', "Is The Sun blue or yellow?" => 'yellow', "Is fire hot or cold?" => 'hot', "Was Einstein clever or stupid?" => 'clever', "Is ice cream hot or cold?" => 'cold' ); $question = array_rand($question_answers); $answer = $question_answers[$question]; Does that work for you? Edit: Fixed Link to comment https://forums.phpfreaks.com/topic/195492-generating-captcha-questions/#findComment-1027292 Share on other sites More sharing options...
kingnutter Posted March 16, 2010 Author Share Posted March 16, 2010 Yes it absolutely does. I had expected $question = array_rand($question_answers) to give me a $row => $value result, not thinking that I could use $question to extract $answer. Thanks! Link to comment https://forums.phpfreaks.com/topic/195492-generating-captcha-questions/#findComment-1027318 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.