dsi36131 2014-04-27 03:55
浏览 49
已采纳

在多维数组的一个维度中查找重复值

I have been searching the archives for the better part of a full day and have not been able to find an answer to my question. I was hoping someone could point me in the right direction.

THE PROBLEM

Without being able to identify teams that are tied (duplicate win percentage) and logic to break those ties (outside scope of this question), I can't test head to head results and ultimately rank the teams correctly.

THE QUESTION

How do I:

  1. Identify which teams have the same win percentage (the number of total games played could differ for some teams)
  2. Count the number of teams that are tied.

EXAMPLE MULTIDIMENSIONAL ARRAY

I have a multidimensional array that stores arrays containing a team number, wins, losses, point differential, and win percentage.

Example:

$team_array = array(
array(68, 6, 0, 10, 1.000),
array(65, 6, 0, 8, 1.000),
array(62, 6, 0, 4, 1.000),
array(54, 3, 3, 3, .500),
array(55, 3, 3, -5, .500),
array(59, 0, 6, -16, .000)
);

I need help finding duplicate values in only 1 dimension of the multidimensional array (win percentage or $team_array[$x][4]. I do not know if it is better to try to find duplicate values or to eliminate unique values.

If anyone has any thoughts or suggestions, I would GREATLY appreciate it.

Thanks in advance!!!!

  • 写回答

1条回答 默认 最新

  • dongwuli5105 2014-04-27 04:39
    关注

    Edit previous code didn't work.

    // $seenDuplicate[ Percentage ][] = indexes which have this percentage.
    foreach(team_array as $ind => $team){
        $trackPercentages[$team[4]][] = $ind;
    }
    // then you can count the number of each array in $trackPercentages
    

    Reply to first comment: it's returning 3 because it contains all percentages regardless of duplication. The first is 1.000, second is .5000 and 3rd is .0000

    foreach($trackPercentages as $perc => $list){
            $echo "Teams with " . $perc . "% wins.<br>";
            foreach($list as $team){
                echo $team."<br>";
            }
            echo "<br>";
    }
    

    if you only want to show duplicates, then right after first foreach loop, check:

    if(count($list)>1)
    

    And I believe ksort can be used to sort an array based on index.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)