Randomly pick element in array then remove from the array

后端 未结 5 756
鱼传尺愫
鱼传尺愫 2020-12-30 03:06

I have an array of phrases. I\'d like to randomly pick phrases from the array in a loop. I don\'t want to pick the same phrase more then once in the loop. I thought I could

5条回答
  •  生来不讨喜
    2020-12-30 04:01

    Shuffle the array in random order, and just pop the last element off.

    $array = [...];
    
    shuffle($array);
    
    while($element = array_pop($array)){
      echo 'Random element:' . $element;
    }
    

提交回复
热议问题