【发布时间】: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