22

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 randomly pick the phrase and then delete it before the next loop.

http://codepad.org/11l0nStX

<?php
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";
}

Is it possible to randomly pick a different phrase from the array on each loop?

2
  • Use something like array_pop which pops and returns the last value of the array, shortening the array by one element. Commented Jun 14, 2013 at 14:27
  • - The actual error in the above code is due to the array being contained within the loop. Each iteration it gets reset. I haven't tested it but that was plainly obvious. Commented Jun 20, 2022 at 16:23

6 Answers 6

47

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I like this approach, simple and clean. The only problem though is that you end up with a shuffled array, so this does not cover all use cases
4

You could also use array_slice

$ran_Num = array_rand($phrases);
$ran_Phrase = array_slice($phrases, $ran_Num, 1);

4 Comments

How can you assure that the one you removed is the one picked
From the doc: "Returns the slice."
Sorry I missed the point. array_rand() returns the index of an indexed array instead of the value at the index.
This is the better solution imho. Note that this returns an array, so you want the first element of that array.
1

You could also use array_rand and array_splice

$array = 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');

$el = array_rand($array);
$dat = $array[$el];
array_splice($array, $el, 1 );

Comments

1

Kinda old question but an easy solution would be to just get random key of the array and then unset it.

  <?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');

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

Comments

0

The other answers here work, but I want to address your code.

<?php

I pulled the definition of $phrases outside of the loop. By setting it inside the loop, it was being reset every time and that's no good.

$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');

I don't like counting, so I let the computer do it.

for($i=0,$n=count($phrases); $i<$n; $i++){

    $ran_Num = array_rand($phrases);
    $ran_Phrase = $phrases[$ran_Num];

When you unset on an array, the value that goes inside the square brackets should be the index of the array element you want to remove, not the value element itself. The variable inside the brackets has been changed from $ran_Phrase to ran_Num

    unset($phrases[$ran_Num]);
    echo $ran_Phrase."\r\n";
    echo count($phrases)."\r\n";
}
?>

Comments

0

Place the selected values an a new array then check if it exists in the new array if not add it.

<?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');

$default = 16;
if($default > ($c = count($phrases))) $default = $c;

$keys = array_rand($phrases, $default);

$newPhrases = array();
foreach($keys as $key){
    if(!isset($newPhrases[$key])){
        $newPhrases[$key] = $phrases[$key];
    }
}
print_r($newPhrases);

3 Comments

in_array and array_rand in a loop would be a bad idea.
I like you're way better. But this is an option.
You could imprivise - use array_flip once at the start and then search like this: if(isset($phrases[$phrase])). It would be much faster, especially in a loop :)

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.