从多维数组中获取前十名列表,而无需重复条目

时间:2019-04-09 12:13:57

标签: php arrays loops multidimensional-array

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

1 个答案:

答案 0 :(得分:2)

source中,您可以尝试创建唯一的多维数组

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