可以使用 array_rand 函数之前生成一个数组索引的数组,然后使用该数组来选择要随机选择的原始数组。
代码示例:
$choices = array(1, 2, 3, 4, 5); $choices2 = array('a', 'b', 'c', 'd', 'e');
$index = array_keys($choices);
for ($i = 0; $i < 5; $i++) { $randomKey = array_rand($index); $choice = $choices[$index[$randomKey]]; $choice2 = $choices2[$index[$randomKey]]; echo "$choice $choice2\n"; }
这个代码示例将输出一个随机选择的数字和一个对应的字母,例如:
2 b 3 c 5 e 4 d 1 a
这个代码示例将随机选择一个数字和一个对应的字母,而不是只返回一个数组的值。