随机顺序的多维数组 [英] multi dimensional array in random order
本文介绍了随机顺序的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要使它的多维数组处于随机顺序.你会怎么做?
I want to make it so that my multi dimensional array is in a random order. How would you do it?
// This is how the array looks like
print_r($slides);
Array
(
[0] => Array
(
[id] => 7
[status] => 1
[sortorder] => 0
[title] => Pants
)
[1] => Array
(
[id] => 8
[status] => 1
[sortorder] => 0
[title] => Jewels
)
[2] => Array
(
[id] => 9
[status] => 1
[sortorder] => 0
[title] => Birdhouse
)
[3] => Array
(
[id] => 10
[status] => 1
[sortorder] => 0
[title] => Shirt
)
[4] => Array
(
[id] => 11
[status] => 1
[sortorder] => 0
[title] => Phone
)
)
// This how the result is if I use array_rand()
print_r(array_rand($slides, 5));
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
// This how the result is if I use shuffle()
print_r(shuffle($slides));
1
推荐答案
shuffle()
是这里的解决方法.因为shuffle
就地更改了数组 并返回布尔值,所以它打印1
并返回布尔值,因为它是用
shuffle()
is the way to go here. It prints 1
because shuffle
changes the array in-place and returns a boolean, as it is written in the documentation:
成功返回 TRUE ,失败返回 FALSE .
我建议您还阅读array_rand()
的文档:
I suggest to also read the documentation of array_rand()
:
从数组中选择一个或多个随机条目,并返回随机条目的键(或多个键).
如果使用内置功能,请务必阅读文档.不要只是假设如何工作.我敢打赌,写问题要比查找问题花更多的时间.
Always read documentation if you use built-in functions. Don't just assume how the work. I bet it took more time to write the question than looking this up.
这篇关于随机顺序的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文