duancheng8000 2019-05-16 22:34
浏览 25
已采纳

需要一种更清洁的方式来打破PHP中的平局

I have a poll which has 4 choices, sometimes people vote for the same things and choices end up in a tie. I'm looking for a way to break this tie.

The poll variables are always different the below is just an example of how they come. I'm currently doing this with messy if statements and randomizing results for each scenario or one === the other.

<?php

$choice1=4;
$choice2=4;
$choice3=2;
$choice4=4;

if ($choice1==$choice2) {
$a = ['$choice1','$choice2'];
$breaker = $a[mt_rand(0, count($a)-1)];
echo $breaker;

}elseif ($choice1==$choice3) {
$a = ['$choice1','$choice3'];
$breaker = $a[mt_rand(0, count($a)-1)];
echo $breaker;

}elseif ($choice1==$choice4) {
$a = ['$choice1','$choice4'];
$breaker = $a[mt_rand(0, count($a)-1)];
echo $breaker;

}elseif ($choice2==$choice1) {
$a = ['$choice2','$choice1'];
$breaker = $a[mt_rand(0, count($a)-1)];
echo $breaker;

//etc...
// This goes on and on also goes up to 3 way ties and 4 way ties.

This method seems extremely inelegant, also the number of votes is going to increase as more and more users register to vote so it is not scalable at this point any suggestions?

  • 写回答

2条回答 默认 最新

  • doutui9606 2019-05-16 23:53
    关注

    This approach uses an array with two set of sorts.

    // As many choices as you would like
    $choices = [
        ['id' => 1, 'name' => 'choice1', 'count' => 4],
        ['id' => 2, 'name' => 'choice2', 'count' => 4],
        ['id' => 3, 'name' => 'choice3', 'count' => 2],
        ['id' => 4, 'name' => 'choice4', 'count' => 4],
        ['id' => 5, 'name' => 'choice5', 'count' => 4],
        ['id' => 6, 'name' => 'choice6', 'count' => 4],
        ['id' => 7, 'name' => 'choice7', 'count' => 4],
        ['id' => 8, 'name' => 'choice8', 'count' => 6],
        ['id' => 9, 'name' => 'choice9', 'count' => 7],
        ['id' => 10, 'name' => 'choice10', 'count' => 4],
        ['id' => 11, 'name' => 'choice11', 'count' => 6],
        ['id' => 12, 'name' => 'choice12', 'count' => 6],
        ['id' => 13, 'name' => 'choice13', 'count' => 7],
        ['id' => 14, 'name' => 'choice14', 'count' => 3],
        ['id' => 15, 'name' => 'choice15', 'count' => 4],
    ];
    
    // First, sort by count
    usort($choices, function($a, $b) {
        if ( $a['count'] < $b['count'] ) return -1; // a < b
        elseif ( $a['count'] > $b['count'] ) return 1; // a > b
        return 0;
    });
    // Now, all are sorted by count.
    // Walk through and deal with items that have the same count.
    $buf = []; // A temp buffer to keep all items with a certain count
    $prevCcount = null;
    $output = [];
    foreach($choices as $choice) {
        $count = $choice['count'];
        echo sprintf('id %d  count %d', $choice['id'], $choice['count']) . "
    ";
        if ( $prevCount != $count ) {
            // Possible new group of items with the same count
            // Does the buffer have more than 1. If so, randomize.
            if ( count($buf) > 1 ) {
                echo "Shuffling " . count($buf) . "items
    ";
                $shuffled = shuffle($buf);
                if ( ! $shuffled ) {
                    throw new Exception('Failed to shuffle');
                }
            }
            if ( count($buf) > 0 ) {
                $output = array_merge($output, $buf);
                $buf = [];
            }
        }
        $prevCount = $count; // Keep track of count of previous item
        $buf[] = $choice; // add current item to buffer
    }
    // Deal with the tail
    // There will be 1 or more items still in the buffer that must be dealt with
    echo "Final buf has " . count($buf) . " items
    ";
    if ( count($buf) > 1 ) {
        echo "Shuffling " . count($buf) . " items
    ";
        $shuffled = shuffle($buf);
        if ( ! $shuffled ) {
            throw new Exception('Failed to shuffle');
        }
    }
    $output = array_merge($output, $buf);
    
    print_r($output);
    echo "
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度