This question already has an answer here:
这个问题已经有了答案:
- multi dimensional array in random order 4 answers
- 多维数组随机排列4个答案。
Is it possible to get random data from an array?
是否可以从数组中获取随机数据?
See My Array:
看到我的数组:
Array ( [0] => Array ( [0] => My Data [1] => Airport [2] => Md ) [1] => Array ( [0] => Live in fear [1] => Science [2] => Sc ) [2] => Array ( [0] => State History [1] => Government [2] => MP ) [3] => Array ( [0] => Real Estate [1] => Other [2] => Property ) [4] => Array ( [0] => Real State [1] => Not Sure [2] => NoData ) )
I need this type of random output...
我需要这种随机输出…
Array ( [0] => Array ( [0] => My Data [1] => Airport [2] => Md ) [1] => Array ( [0] => Real State [1] => Not Sure [2] => NoData ) [2] => Array ( [0] => My Data [1] => Airport [2] => Md ) [3] => Array ( [0] => State History [1] => Government [2] => MP ) [4] => Array ( [0] => Live in fear [1] => Science [2] => Sc ) )
3 个解决方案
#1
0
You could simply use shuffle()
您可以简单地使用shuffle()
bool shuffle ( array &$array )
bool shuffle(数组&$array)
This function shuffles (randomizes the order of the elements in) an array. It uses a pseudo random number generator that is not suitable for cryptographic purposes.
这个函数会打乱数组中的元素顺序。它使用一个伪随机数生成器,不适合加密目的。
shuffle($array); // Shuffles your array keys randomly every time.
#2
0
shuffle() will be a better option in getting out the random value from multi dimensional array.
shuffle()将是一个更好的选择,从多维数组中获取随机值。
Reference: http://php.net/manual/en/function.shuffle.php
参考:http://php.net/manual/en/function.shuffle.php
shuffle() Example:
洗牌()例子:
The shuffle() function randomizes the order of the elements in the array.
函数的作用是:随机化数组中元素的顺序。
This function assigns new keys for the elements in the array. Existing keys will be removed
这个函数为数组中的元素分配新的键。现有的密钥将被删除。
<?php
$my_array = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"purple");
shuffle($my_array);
print_r($my_array);
?>
Output:
输出:
Array ( [0] => red [1] => yellow [2] => green [3] => blue [4] => purple )
//The Output will keep shuffling if you refresh the browser.
#3
0
Try the following shuffle function.Hope it would help you.
试试下面的shuffle功能。希望它能帮到你。
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[] = $list[$key];
}
return $random;
}
$arr = array();
$arr[] = array('id' => 50, 'foo' => 'hello');
$arr[] = array('id' => 17, 'foo' => 'byebye');
$arr[] = array('id' => 19, 'foo' => 'foo');
print_r(shuffle_assoc($arr));
#1
0
You could simply use shuffle()
您可以简单地使用shuffle()
bool shuffle ( array &$array )
bool shuffle(数组&$array)
This function shuffles (randomizes the order of the elements in) an array. It uses a pseudo random number generator that is not suitable for cryptographic purposes.
这个函数会打乱数组中的元素顺序。它使用一个伪随机数生成器,不适合加密目的。
shuffle($array); // Shuffles your array keys randomly every time.
#2
0
shuffle() will be a better option in getting out the random value from multi dimensional array.
shuffle()将是一个更好的选择,从多维数组中获取随机值。
Reference: http://php.net/manual/en/function.shuffle.php
参考:http://php.net/manual/en/function.shuffle.php
shuffle() Example:
洗牌()例子:
The shuffle() function randomizes the order of the elements in the array.
函数的作用是:随机化数组中元素的顺序。
This function assigns new keys for the elements in the array. Existing keys will be removed
这个函数为数组中的元素分配新的键。现有的密钥将被删除。
<?php
$my_array = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"purple");
shuffle($my_array);
print_r($my_array);
?>
Output:
输出:
Array ( [0] => red [1] => yellow [2] => green [3] => blue [4] => purple )
//The Output will keep shuffling if you refresh the browser.
#3
0
Try the following shuffle function.Hope it would help you.
试试下面的shuffle功能。希望它能帮到你。
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[] = $list[$key];
}
return $random;
}
$arr = array();
$arr[] = array('id' => 50, 'foo' => 'hello');
$arr[] = array('id' => 17, 'foo' => 'byebye');
$arr[] = array('id' => 19, 'foo' => 'foo');
print_r(shuffle_assoc($arr));