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

时间:2022-03-28 07:38:55

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. 确定哪些球队拥有相同的胜率(一些球队的比赛总数可能会有所不同)

  3. Count the number of teams that are tied.
  4. 计算绑定的团队数量。

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.

我需要帮助在多维数组的一维中找到重复值(赢率百分比或$ team_array [$ x] [4]。我不知道尝试查找重复值或消除唯一值是否更好。

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

如果有人有任何想法或建议,我会非常感激。

Thanks in advance!!!!

提前致谢!!!!

1 个解决方案

#1


0  

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

回复第一条评论:它返回3,因为它包含所有百分比而不管重复。第一个是1.000,第二个是.5000,第三个是.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:

如果你只想显示重复项,那么在第一个foreach循环后,检查:

if(count($list)>1)

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

我相信ksort可以用来根据索引对数组进行排序。

#1


0  

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

回复第一条评论:它返回3,因为它包含所有百分比而不管重复。第一个是1.000,第二个是.5000,第三个是.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:

如果你只想显示重复项,那么在第一个foreach循环后,检查:

if(count($list)>1)

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

我相信ksort可以用来根据索引对数组进行排序。