php - 从多维数组中获取前十名列表而没有重复条目
问题描述
我尝试从我从 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>
解决方案
从源代码,您可以尝试这个来制作唯一的多维数组
$a = topTenGoalGetter();
$input = array_slice(array_values(array_map("unserialize", array_unique(array_map("serialize", $a)))),0,10);
推荐阅读
- javascript - 如何从 getElementById 获得多个唯一结果?JavaScript
- bash - 将两行的部分提取为一行
- php - 如何在我的 order.php 中插入产品名称、单位名称、价格
- r - 用另一个日期字段替换空白日期字段
- c - 返回 int 的函数上的 C“不应忽略的 void 值”错误
- c# - 使用正则表达式在 C# 中解析字符串
- pdf - 仅禁用嵌入式 PDF 打印和下载选项
- phpmailer - 通过没有 smtp-auth 和代理的 emote sendmail 发送电子邮件
- r - mapedit改变颜色和不透明度
- sql - Oracle 中的 SELECT 计算列