简体   繁体   English

随机数组

[英]Randomize array

How would I randomize this Array so that when I use a foreach loop it would it would switch index 0 with key 1 and to randomize the order? 我将如何随机化此数组,以便在使用foreach循环时将使用键1切换索引0并随机化顺序?

I have tried $variable= array_rand($variable,count($variable)) but it prints out 我试过$variable= array_rand($variable,count($variable))但它打印出来

  0 => int 0
  1 => int 1
  2 => int 2
  3 => int 3
  4 => int 4

Here is my code: 这是我的代码:

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)

I need to randomize the order so that when it prints it will print a new order everytime. 我需要将订单随机化,以便在打印时每次都会打印一个新订单。

The array stores question and question option in the keys/indexes 数组将问题和问题选项存储在键/索引中

If all you are looking to achieve is to shuffle the values of an array without any need to keep them associated with their original keys, you might want to consider looking at the shuffle() function. 如果您要实现的全部目的是在不需将其值与原始键关联的情况下将数组的值改组,则可能需要考虑使用shuffle()函数。

This function takes an array by reference (note the argument &$array in the docs for this function) so it will shuffle the array you pass in and return a boolean relating to its success. 此函数按引用接受数组(请注意此函数的文档中的参数&$array ),因此它将混洗您传入的数组并返回与其成功相关的布尔值。

As an example: 举个例子:

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

At this stage, the array is in the order that we initialised it in. If we use shuffle() and give it $myArray as the argument, we will end up with a different order: 在此阶段,数组按照我们对其初始化的顺序进行。如果我们使用shuffle()并将其赋予$myArray作为参数,我们将以不同的顺序结束:

shuffle($myArray);

print_r($myArray); //Might print: Array ( [0] => elephant [1] => toadstool [2] => banana [3] => orange )

If you ran shuffle again you would get a potentially different order. 如果您再次洗牌,则可能会获得不同的订单。 (Why ' potentially '?! Well, because it may randomly select the same order again especially if the array is small like this example one). (为什么“ 潜在地 ”?!好吧,因为它可能会再次随机选择相同的顺序,尤其是在数组较小的情况下(如本示例所示)。

It is worth noting that: 值得一提的是:

[Shuffle] uses a pseudo random number generator that is not suitable for cryptographic purposes. [Shuffle]使用了不适合加密目的的伪随机数生成器。

Source: Shuffle Docs 资料来源: Shuffle Docs


Considerations: 注意事项:

If you didn't want to change the order of your array you could use something like array_rand() which would return you random keys from your array which you could then look up. 如果您不想更改数组的顺序,则可以使用诸如array_rand() ,该方法将从您的数组中返回随机键,然后您可以进行查找。 It's the same idea except the array is never changed. 这是相同的想法,只是数组从未更改。

Assuming the array above: 假设上面的数组:

$randomKey = array_rand($myArray); //Random key is now 2

echo "My random array value is: ".$myArray[$randomKey]; //Prints: My random array value is: orange

As the manual tells us: 手册告诉我们:

This function assigns new keys to the elements in array. 此函数将新键分配给数组中的元素。 It will remove any existing keys that may have been assigned, rather than just reordering the keys. 它将删除可能已经分配的所有现有密钥,而不仅仅是重新排序密钥。

How does this affect me? 这对我有什么影响? Well if you had specific keys like this: 好吧,如果您有这样的特定键:

$myArray = array(
    "firstFruit" => "banana", 
    "anotherFruit" => "orange", 
    "animal" => "elephant", 
    "plant"=>"toadstool"
);

After your first call to shuffle() your array would look like this: 在第一次调用shuffle()您的数组将如下所示:

Array ( 
    [0] => orange 
    [1] => toadstool 
    [2] => banana 
    [3] => elephant 
)

and your associated keys are lost! 并且您关联的密钥丢失了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM