如何停止再次打印相同的随机文本字符串


How can I stop printing out the same random text string again?

下面的PHP代码随机打印出6个不同的"测试"字符串中的一个。

如何在不再次显示第一个字符串的情况下第二次打印出"测试"字符串?

<?php
$Array[] = 'test1';
$Array[] = 'test2';
$Array[] = 'test3';
$Array[] = "test4";
$Array[] = "test5";
$Array[] = "test6";
$RandomIndex =  rand(0,sizeof($Array)-1); 
echo $Array[$RandomIndex];
?>

只需shuffle,然后删除一个(array_pop)并返回:

$Array[] = 'test1';
$Array[] = 'test2';
$Array[] = 'test3';
$Array[] = "test4";
$Array[] = "test5";
$Array[] = "test6";
shuffle($Array);
echo array_pop($Array);
echo array_pop($Array);

如果你想跨页面浏览,请将其放入会话:

session_start();
$_SESSION['Array'] = $Array;
shuffle($_SESSION['Array']);
echo array_pop($_SESSION['Array']);
echo array_pop($_SESSION['Array']);

试着分享的结果

for($i = 1; $i <= 6; ){
  $RandomIndex =  rand(0,sizeof($Partner)-1);
  if(!array_key_exists($RandomIndex , $Array)){
    echo $Array[$RandomIndex];
  } $i++;
}