ttmt
ttmt

Reputation: 4984

Randomly pick element in array then remove from the array

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?

Upvotes: 21

Views: 15249

Answers (6)

Jawido Kakarot
Jawido Kakarot

Reputation: 86

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";

Upvotes: 1

Toto
Toto

Reputation: 91488

You could also use array_slice

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

Upvotes: 4

Get Off My Lawn
Get Off My Lawn

Reputation: 36341

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

Upvotes: 0

terite
terite

Reputation: 121

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

Upvotes: 0

Orangepill
Orangepill

Reputation: 24655

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

Upvotes: 1

Mārtiņš Briedis
Mārtiņš Briedis

Reputation: 17762

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;
}

Upvotes: 46

Related Questions