我如何随机化这个数组,以便当我使用 foreach 循环时,它将用键 1 切换索引 0 并随机化顺序?
我已经尝试过
$variable= array_rand($variable,count($variable))
但它打印出来了
0 => int 0
1 => int 1
2 => int 2
3 => int 3
4 => int 4
这是我的代码:
foreach ($variable as $key)
array (size=10)
0 =>
object(stdClass)[25]
public 'id' => string '24' (length=2)
public 'course_name' => string 'Office Automation' (length=17)
public 'test_name' => string 'Test 2' (length=6)
public 'total_questions' => string '10' (length=2)
public 'duration' => string '20' (length=2)
public 'total_marks' => string '20' (length=2)
public 'question' => string 'Question1' (length=9)
public 'option1' => string 'ans1' (length=4)
public 'option2' => string 'ans2' (length=4)
public 'option3' => string 'ans3' (length=4)
public 'option4' => string 'ans4' (length=4)
public 'ans' => string 'D' (length=1)
public 'count' => string '1' (length=1)
1 =>
object(stdClass)[26]
public 'id' => string '25' (length=2)
public 'course_name' => string 'Office Automation' (length=17)
public 'test_name' => string 'Test 2' (length=6)
public 'total_questions' => string '10' (length=2)
public 'duration' => string '20' (length=2)
public 'total_marks' => string '20' (length=2)
public 'question' => string 'Question2' (length=9)
public 'option1' => string 'ans1' (length=4)
public 'option2' => string 'ans2' (length=4)
public 'option3' => string 'ans3' (length=4)
public 'option4' => string 'ans4' (length=4)
public 'ans' => string 'A' (length=1)
public 'count' => string '2' (length=1)
2 =>
object(stdClass)[27]
public 'id' => string '26' (length=2)
public 'course_name' => string 'Office Automation' (length=17)
public 'test_name' => string 'Test 2' (length=6)
public 'total_questions' => string '10' (length=2)
public 'duration' => string '20' (length=2)
public 'total_marks' => string '20' (length=2)
public 'question' => string 'Question3' (length=9)
public 'option1' => string 'ans1' (length=4)
public 'option2' => string 'ans2' (length=4)
public 'option3' => string 'ans3' (length=4)
public 'option4' => string 'ans4' (length=4)
public 'ans' => string 'B' (length=1)
public 'count' => string '3' (length=1)
我需要随机化订单,以便在打印时每次都会打印新订单。
数组在键/索引中存储问题和问题选项。
如果您想要实现的只是打乱数组的值,而不需要将它们与原始键相关联,那么您可能需要考虑查看
shuffle()
函数。
此函数通过引用获取一个数组(请注意此函数文档中的参数
&$array
),因此它将对您传入的数组进行洗牌并返回与其成功相关的布尔值。
举个例子:
// Setup an array filled with values:
$myArray = array("banana", "orange", "elephant", "toadstool");
print_r($myArray); //Returns: Array ( [0] => banana [1] => orange [2] => elephant [3] => toadstool )
shuffle()
并给它 $myArray
作为参数,我们最终会得到不同的顺序:
shuffle($myArray);
print_r($myArray); //Might print: Array ( [0] => elephant [1] => toadstool [2] => banana [3] => orange )
如果您再次运行随机播放,您可能会得到不同的顺序。 (为什么‘潜在’?!好吧,因为它可能会再次随机选择相同的顺序,特别是如果数组很小,就像这个例子一样)。
值得注意的是:
[Shuffle] 使用不适合加密目的的伪随机数生成器。
来源:Shuffle 文档
array_rand()
的内容,它会从数组中返回随机键,然后您可以查找这些键。除了数组从未改变之外,这是相同的想法。
假设上面的数组:
$randomKey = array_rand($myArray); //Random key is now 2
echo "My random array value is: ".$myArray[$randomKey]; //Prints: My random array value is: orange
正如手册告诉我们的:
该函数为数组中的元素分配新的键。它将删除可能已分配的任何现有键,而不仅仅是重新排序键。
这对我有何影响? 好吧,如果你有这样的特定键:
$myArray = array(
"firstFruit" => "banana",
"anotherFruit" => "orange",
"animal" => "elephant",
"plant"=>"toadstool"
);
第一次调用
shuffle()
后,您的数组将如下所示:
Array (
[0] => orange
[1] => toadstool
[2] => banana
[3] => elephant
)
并且您的关联密钥丢失了!