简体   繁体   English

获取具有子元素的数组元素,而无需在PHP中重复

[英]Get array element with sub elements without repeating in PHP

I walk around here with some hesitation, I have passed an array with sub elements (so to speak) and I need three random values ​​but these are obtained without repeating. 我有些犹豫,在这里走来走去,我已经传递了一个带有子元素的数组(可以这么说),我需要三个随机值,但是这些都是随机获得的。

The array is as follows: 数组如下:

Array
    (
        [0] => Array
            (
                [uid] => 1
                [ticket_code] => 0oreb8yo
            )

        [1] => Array
            (
                [uid] => 1
                [ticket_code] => 2oeii8hm
            )

        [2] => Array
            (
                [uid] => 1
                [ticket_code] => m0dwtjiw
            )

        [3] => Array
            (
                [uid] => 1
                [ticket_code] => q6c7cymb
            )

        [4] => Array
            (
                [uid] => 1
                [ticket_code] => zyqhm5bj
            )

        [5] => Array
            (
                [uid] => 1
                [ticket_code] => amdqzjpi
            )

        [6] => Array
            (
                [uid] => 2
                [ticket_code] => tzql7l42
            )

        [7] => Array
            (
                [uid] => 2
                [ticket_code] => gap0r6vf
            )

        [8] => Array
            (
                [uid] => 2
                [ticket_code] => ypqum5yz
            )

        [9] => Array
            (
                [uid] => 4
                [ticket_code] => smupluac
            )

        [10] => Array
            (
                [uid] => 4
                [ticket_code] => 9d8jsha7
            )

        [11] => Array
            (
                [uid] => 5
                [ticket_code] => 6hdnja42
            )

    )

And I need you to get 3 "ticket_code" but no right to repeat the "uid". 我需要您获得3个“ ticket_code”,但无权重复“ uid”。

I've been on trying as follows, but also repeats the "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']
    );
  }

Any way to do it without repeats? 有没有办法重复做?

We thank you in advance if anyone knows of something ^^ 如果有人知道我们会先谢谢您^^

Try something like: 尝试类似:

$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);

if you're afraid of infinite loops (that can take place really) then try this: 如果您担心无限循环(可能确实发生),请尝试以下操作:

$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;
}

Here in worst case you check $allTickets array and get winners of size <= 3 . 在最坏的情况下,您检查$allTickets数组并获得大小为<= 3获胜者。

Try this: 尝试这个:

$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--;
    }
}

this structure would be better ( added benefit of ticket numbers being unique ) 这种结构会更好(机票号码唯一的额外好处)

$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);

outputs 输出

array (
  0 => 2,
  1 => 1,
  2 => 4,
)

array (
  0 => 2,
  1 => 1,
  2 => 3,
)

array (
  0 => 1,
  1 => 3,
  2 => 2,
)

unlike the while option, this will reduce the operations for each loop of the for loop by reducing the ticket array by the uid. 与while选项不同,这将通过减少uid的票证数组来减少for循环的每个循环操作。 It's also the only way to insure your not always pulling out a user with tickets, what if user 1 bought 90% of the tickets, you'd loop on him 90% of the time, in any case you have to reduce the ticket array by winners if they can win only once. 这也是确保您不总是抽出用户票证的唯一方法,如果用户1购买了90%的票证,您将有90%的时间向他循环,无论如何,您都必须减少票证阵列如果获胜者只能赢一次,则由获胜者决定。 In essence you remove each uid from the list when they win. 本质上,当每个uid赢时,您将从列表中删除它们。 You can also be sure that each ticket has the same chance to win ( as well as array_rand is random that is ) - they all have equal footing. 您还可以确保每张彩票都有相同的中奖机会(以及array_rand是随机的)-它们都有平等的立足点。

ticket array reduction after loop1 loop1之后的票证数组减少

array (
  'tzql7l42' => 2,
  'gap0r6vf' => 2,
  'ypqum5yz' => 2,
  'smupluac' => 3,
  '9d8jsha7' => 4,
  '6hdnja42' => 5,
)

after loop2 在loop2之后

array (
  'smupluac' => 3,
  '9d8jsha7' => 4,
  '6hdnja42' => 5,
)

after loop3 在loop3之后

array (
  'smupluac' => 3,
  '6hdnja42' => 5,
)

winners 获奖者

array (
  0 => 1,
  1 => 2,
  2 => 4,
)

to return both the uid and wining ticket change 返回uid和中票变更

$winners[] = $winner;

to

$winners[$ticket] = $tickets[$ticket];

now winners will be, just like the input array 现在赢家将会像输入数组一样

ticketnumber => uid

ticket is the key ( which is the ticket ) and winner is the value ( which is the uid ) 票证是键(即票证),获胜者是值(即uid)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM