【问题标题】:getting top ten list from multidimensional array without duplicate entries从多维数组中获取前十名列表而没有重复条目
【发布时间】:2019-04-09 12:13:57
【问题描述】:

我尝试从我从 API 获得的多维数组中获取前十名列表。我已经尝试过在 stackoverflow 上给出的解决方案,它有点工作。它给了我数组的前十个条目。剩下的唯一问题是,有重复的条目。

我试过array_unique,但它不起作用。您可以在代码示例中看到它。我不想删除重复的条目。所以有一个前十名守门员的名单。我想要的列表如下所示:

 Name - 17 Goals
 Name - 10 Goals
 Name - 10 Goals
 Name - 9 Goals

等等。

所以前十名包括射门数相同的人。我希望它能很好地解释它。

我得到了前十个值的 18 多个条目的列表。考虑到重复目标,我如何获得前十个值?

<?php
function topTenGoalGetter()
{
    $json_file     = @file_get_contents('https://www.openligadb.de/api/getgoalgetters/bl1/2018');
    $entries       = json_decode($json_file, true);
    $goalgetter    = $entries;
    $return_topten = array();
    $goals         = array();
    foreach ($entries as $entry) {
        array_push($goals, $entry['GoalCount']);
    }
    $total    = count($goals);
    $counter  = 1;
    $for_show = 10;
    while ($counter <= $total - $for_show) {
        $counter++;
        $key = array_search(min($goals), $goals);
        unset($goals[$key]);
    }
    foreach ($entries as $entry) {
        foreach ($goals as $key => $value) {
            if ($entry["GoalCount"] == $value) {
                array_push($return_topten, $entry);
            }
        }
    }
    return $return_topten;
}
?>
<div class="bl-torschuetzen">
    <div class="bl-torschuetzen-entries">
        <span>Test</span>
        <pre>
            <?php var_dump(topTenGoalGetter());?>
        </pre>
    </div>
</div>

【问题讨论】:

  • 问题是 API 返回不一致的进球者姓名,例如 Robert Lewandowski 我发现 3 个不同的名字相同的球员 Robert Lewandowski | Lewandowski | Lewandowski, Robert 都有不同的 GoalGetterId

标签: php arrays loops multidimensional-array


【解决方案1】:

来自source,你可以试试这个来制作唯一的多维数组

$a = topTenGoalGetter();
$input = array_slice(array_values(array_map("unserialize", array_unique(array_map("serialize", $a)))),0,10);

【讨论】:

  • 我第一次实施这个错误。现在它起作用了。我得到了预期的清单。谢谢!编辑:我仍然有 12 个条目。
  • 无法发布输出,因为它太长了
  • 你是说,你还没得到答案?
  • 遗憾的是没有。我得到 12 个结果,我期待 10 个。如果我将计数器设置为 5,我得到预期的 5 个结果。我一无所知
  • 行得通!谢谢你。我将此标记为正确答案!
猜你喜欢
  • 1970-01-01
  • 2019-05-20
  • 2011-10-06
  • 1970-01-01
  • 2019-12-14
  • 1970-01-01
  • 2015-05-27
  • 2017-05-15
  • 1970-01-01
相关资源
最近更新 更多