1

I'm attempting to make a program along the same lines as this. I am trying to randomely select an element from the array, display it, and then remove it from the array. However, the element is not displaying.

    for($i=0; $i<16; $i++){

        $phrases = array('Hello Sailor','Acid Test','Bear Garden','Botch A Job','Dark Horse',
                'In The Red','Man Up','Pan Out','Quid Pro Quo','Rub It In','Turncoat',
                'Yes Man','All Wet','Bag Lady','Bean Feast','Big Wig');

    $ran_Num = array_rand($phrases);
    $ran_Phrase = $phrases[$ran_Num];
    unset($phrases[$ran_Phrase]);   
    echo $ran_Phrase."\r\n";      
    echo count($phrases)."\r\n";

    }

?>
1
  • 1
    Could you please clarify what your issue is, what language you are asking for assistance in (html is just a markup text. You would need a programming language such as javascript to do what you are asking), and post any code you have already, so we can see where you have gone wrong. Commented Dec 8, 2016 at 2:43

1 Answer 1

1
<?php
$phrases = array('Hello Sailor','Acid Test','Bear Garden','Botch A Job','Dark Horse',
                 'In The Red','Man Up','Pan Out','Quid Pro Quo','Rub It In','Turncoat',
                 'Yes Man','All Wet','Bag Lady','Bean Feast','Big Wig');
for($i=0; $i<16; $i++){ 
    $ran_Num = array_rand($phrases);
    $ran_Phrase = $phrases[$ran_Num];
    echo $ran_Phrase."<br>";      
    echo count($phrases)."<br>";
    unset($phrases[$ran_Num]);
}
?>

Brought the array outside the loop (since if it's inside, it'll always reset to 16 items).
Replaced $phrases[$ran_Phrase] to $phrases[$ran_Num] because we unset index.

EDIT:

PHPFiddle - Demo: http://phpfiddle.org/main/code/pw2f-qrp3

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

Comments

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.