随机化数组

时间:2016-08-04 09:35:22

标签: php arrays codeigniter random

我如何随机化这个数组,这样当我使用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)

我需要随机化订单,以便在打印时它会每次都打印一个新订单。

数组在键/索引

中存储问题和问题选项

1 个答案:

答案 0 :(得分: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]使用不适合加密目的的伪随机数生成器。

来源:Shuffle Docs

注意事项:

如果您不想更改阵列的顺序,可以使用类似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

正如the manual告诉我们的那样:

  

此函数为数组中的元素指定新键。它将删除可能已分配的任何现有密钥,而不仅仅是重新排序密钥。

这对我有何影响? 好吧,如果你有这样的特定键:

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

第一次调用shuffle()后,您的数组将如下所示:

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

并且您的相关密钥丢失了!

相关问题