我带着一些犹豫走来走去,我已经传递了一个带有子元素的数组(可以这么说),我需要三个随机值,但这些值是在没有重复的情况下获得的。
数组如下:
<>之前数组([0] =>数组([uid] => 1[ticket_code] => 0oreb8yo)[1] =>数组([uid] => 1[ticket_code] => 20eii8hm)[2] =>数组([uid] => 1[ticket_code] => m0dwtjiw)[3] =>阵列([uid] => 1[ticket_code] => q6c7cymb)[4] =>数组([uid] => 1[ticket_code] => zyqhm5bj)[5] =>数组([uid] => 1[ticket_code] => amdqzjpi)[6] =>数组([uid] => 2[ticket_code] => tzql7l42)[7] =>数组([uid] => 2[ticket_code] => gap0r6vf)[8] =>数组([uid] => 2[ticket_code] => ypqum5yz)[9] =>数组([uid] => 4[ticket_code] => smupluac)[10] =>数组([uid] => 4[ticket_code] => 9d8jsha7)[11] =>数组([uid] => 5[ticket_code] => 6hdnja42))之前我需要你得到3个"ticket_code",但没有权利重复"uid"。
我一直在尝试如下,但也重复"uid"。
$ticketsWinners = array();
for ($i=0; $i < 3; $i++) {
$aux = array_rand($allTickets);
$aux2 = $allTickets[$aux]['uid'];
$ticketsWinners[] = array(
'uid' => $aux2,
'ticket_code' => $allTickets[$aux]['ticket_code']
);
}
有办法不重复做吗?
如果有人知道什么,我们提前感谢你^^
试试这样写:
$ticketsWinners = array();
while (sizeof($ticketsWinners) < 3) {
$aux = array_rand($allTickets);
// array_rand return array of keys so you need first value only
$uid = $allTickets[$aux[0]]['uid']
// add uid as a key so ass not tot check all $allTickets values
if (!isset($ticketsWinners[$uid]))
$ticketsWinners[$uid] = $allTickets[$aux[0]];
}
// if you need $allTickets back to numeric keys [0, 1, 2]
$allTickets = array_values($allTickets);
如果你害怕无限循环(这真的可能发生),那么试试这个:
$ticketsWinners = array();
// shuffle array before checking
shuffle($allTickets);
foreach ($allTickets as $tick_data) {
$uid = $tick_data['uid'];
if (!isset($ticketsWinners[$uid]))
$ticketsWinners[$uid] = $tick_data;
if (sizeof($ticketsWinners) == 3)
break;
}
在最坏的情况下,您检查$allTickets
数组并获得大小为<= 3
的获胜者
试试这个:
$ticketsWinners = array();
$ticketUid = array();
for ($i=0; $i < 3; $i++) {
$aux = array_rand($allTickets);
$aux2 = $allTickets[$aux]['uid'];
if(! in_array($aux2, $ticketUid)) {
$ticketUid[$i] = $aux2;
$ticketsWinners[] = array(
'uid' => $aux2,
'ticket_code' => $allTickets[$aux]['ticket_code']
);
} else {
$i--;
}
}
这种结构会更好(增加了票号唯一的好处)
$tickets = Array
(
'0oreb8yo' => 1,
'2oeii8hm' => 1,
'm0dwtjiw' => 1,
'q6c7cymb' => 1,
'zyqhm5bj' => 1,
'amdqzjpi' => 1,
'tzql7l42' => 2,
'gap0r6vf' => 2,
'ypqum5yz' => 2,
'smupluac' => 3,
'9d8jsha7' => 4,
'6hdnja42' => 5,
);
$winners = array();
$picks = 3;
for($i = 0; $i < $picks; $i++){
if(count($tickets) == 0 ){
break; //or error -- shouldn't need this unless picks exceed uids
}
$ticket = array_rand($tickets);
$winner = $tickets[$ticket];
$winners[] = $winner;
$tickets = array_filter($tickets, function($item) use ($winner){
return $winner != $item;
});
}
echo '<pre>';
var_export($winners);
输出array (
0 => 2,
1 => 1,
2 => 4,
)
array (
0 => 2,
1 => 1,
2 => 3,
)
array (
0 => 1,
1 => 3,
2 => 2,
)
与while选项不同,这将通过减少票证数组的uid来减少for循环的每个循环的操作。这也是确保你不会总是抽到有彩票的用户的唯一方法,如果用户1买了90%的彩票,你会在他身上循环90%的时间,在任何情况下,你必须减少赢家的彩票数组如果他们只能赢一次。实际上,当每个uid获胜时,您将从列表中删除它们。您还可以确保每张彩票都有相同的获胜机会(也就是说,array_rand是随机的)—它们都具有相同的基础。
票证数组缩减后loop1
array (
'tzql7l42' => 2,
'gap0r6vf' => 2,
'ypqum5yz' => 2,
'smupluac' => 3,
'9d8jsha7' => 4,
'6hdnja42' => 5,
)
loop2后array (
'smupluac' => 3,
'9d8jsha7' => 4,
'6hdnja42' => 5,
)
loop3后array (
'smupluac' => 3,
'6hdnja42' => 5,
)
赢家array (
0 => 1,
1 => 2,
2 => 4,
)
返回uid和中奖票更改
$winners[] = $winner;
$winners[$ticket] = $tickets[$ticket];
现在赢家将是,就像输入数组
一样ticketnumber => uid
ticket是键(也就是彩票),winner是值(也就是uid)