在数组中随机选择元素,然后从数组中删除

时间:2022-06-02 21:22:23

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

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.

是否可以从每个循环的数组中随机选择不同的短语?

5 个解决方案

#1


25  

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

#2


3  

You could also use array_slice

你也可以使用array_slice。

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

#3


0  

You could also use array_rand and array_splice

您还可以使用array_rand和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 );

#4


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.

我在循环之外提取了$phrase的定义。通过在循环中设置它,它每次都被重置,这是不好的。

$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

当在数组上取消设置时,方括号内的值应该是要删除的数组元素的索引,而不是值元素本身。括号内的变量已从$ran_Phrase改为ran_Num。

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

#5


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

#1


25  

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

#2


3  

You could also use array_slice

你也可以使用array_slice。

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

#3


0  

You could also use array_rand and array_splice

您还可以使用array_rand和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 );

#4


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.

我在循环之外提取了$phrase的定义。通过在循环中设置它,它每次都被重置,这是不好的。

$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

当在数组上取消设置时,方括号内的值应该是要删除的数组元素的索引,而不是值元素本身。括号内的变量已从$ran_Phrase改为ran_Num。

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

#5


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