我试图按照与this相同的方式制作一个程序。我试图从数组中选择一个元素,显示它,然后从数组中删除它。但是,该元素未显示。
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";
}
?>
答案 0 :(得分:1)
<?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');
for($i=0; $i<16; $i++){
$ran_Num = array_rand($phrases);
$ran_Phrase = $phrases[$ran_Num];
echo $ran_Phrase."<br>";
echo count($phrases)."<br>";
unset($phrases[$ran_Num]);
}
?>
将数组带到循环外(因为如果它在里面,它将总是重置为16项)
将$phrases[$ran_Phrase]
替换为$phrases[$ran_Num]
,因为我们取消了索引。
编辑: