PHP security Questions creation ? { Random Arrays }

Discussion in 'PHP' started by xkaser101, Aug 8, 2011.

  1. #1
    Hello,
    I have no idea how to pick a random key from an array with it's value :mad:

    I want to create a very simple security system to prevent spam by asking users simple questions such as "Fire is Hot or Cold" might Be hard but hey life is not fair :cool:

    Anyway i am really stuck at how to do this I've used array_rand and managed to get a random question out of my array :

    
    <?php 
    $questions_array = array('Fire is hot or cold' => 'hot', 'The moon is red or white' => 'white',....ETC); 
    $rand_question = array_rand($questions_array);
    ?>
    
    Code (markup):
    But I am unable to get the answer along with the random question, Any Suggestions ? :confused:

    Thanks in advance,
    Kind Regards,
    Issa Shamoun
     
    Solved! View solution.
    xkaser101, Aug 8, 2011 IP
  2. #2
    maby try this;

    
    $random_array_key = array_rand($your_array);
    $random_array_value = $your_array[$random_array_key];
    
    Code (markup):
     
    EricBruggema, Aug 8, 2011 IP
  3. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #3
    <?php 
    
    $questions_array = array('Fire is hot or cold' => 'hot', 'The moon is red or white' => 'white');
    
    $random_array_key = array_rand($questions_array);
    $random_array_value = $questions_array[$random_array_key];
    
    echo $random_array_key. ' => ' . $random_array_value;
    
    ?>
    PHP:
    Just putting what EricBruggema put but making it output for you.
     
    exodus, Aug 8, 2011 IP
  4. xkaser101

    xkaser101 Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you ! got it working perfectly !!
     
    xkaser101, Aug 8, 2011 IP