获取带有子元素的数组元素,而不是在PHP中重复

时间:2014-07-06 06:22:50

标签: php arrays array-unique

我带着一些犹豫在这里走来走去,我已经传递了一个带有子元素的数组(可以这么说),我需要三个随机值,但这些都是在不重复的情况下获得的。

数组如下:


    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
            )

    )

我需要你得到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']
    );
  }

没有重复的任何方式吗?

如果有人知道某事,我们会提前感谢你^^

3 个答案:

答案 0 :(得分:1)

尝试类似:

$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的获胜者。

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

这种结构会更好(票号的独特优势是唯一的)

$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和Wining票证更改

$winners[] = $winner;

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

现在获胜者将会像输入数组一样

ticketnumber => uid

票证是关键(票证),赢家是值(即uid)