I have an array:

$input2 = array("S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10"). 

I need to remove a random element from there from an array and display this element on the screen and then remove this element from the array.

    2 answers 2

     $i = rand(0, count($input2) - 1); echo $input2[$i]; unset($input2[$i]); 
    • 3
      I wanted to add header('Location: http://php.net/docs.php'); at the end header('Location: http://php.net/docs.php'); ... barely resist -)) - AlexandrX Nov.
    • one
      If I am not mistaken, unset() does not shift indices. And, you may still need to call array_merge() so that the indices go without gaps - Anton Shchyrov
    • Well, it wasn’t like to say in TK that indexes should not be thinned out :) - AlexandrX
    • Well, that is, if in fact - you are right, repeated calls will fail - AlexandrX
     $arr = ["S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10"]; while($arr) { shuffle($arr); //перемешивает массив echo array_pop($arr); //извлекает последний элемент массива уменьшая его //можно так же использовать array_shift() }