首页 > 解决方案 > 从多维数组中获取前十名列表而没有重复条目

问题描述

我尝试从我从 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>

标签: phparraysloopsmultidimensional-array

解决方案


源代码,您可以尝试这个来制作唯一的多维数组

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

推荐阅读