0

i try to get a top ten list from an multidimensional array i get from an API. I've tried a solution given on stackoverflow and it kinda works. It gives me the top ten entries of the array. The only problem left is, that there are duplicate entries.

I've tried array_unique, but it doesnt work. You see it in the code example. I dont want duplicate entries to deleted. So theres a list of top ten goalgetters. The list i want looks like this:

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

and so on.

So the top ten includes the ones who shot the same amount of goals. I hope it explains it well enough.

I get a list of 18+ entries of the top ten values. How can i get the top ten values with the duplicate goals in mind?

<?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>
Rahul
  • 18,271
  • 7
  • 41
  • 60
  • 1
    the problem is the api returning inconsistent goal scorers' names for example ```Robert Lewandowski``` i found 3 different names same player i guess ```Robert Lewandowski | Lewandowski | Lewandowski, Robert ``` all with different ```GoalGetterId``` – Talal Apr 09 '19 at 12:34

1 Answers1

1

From source, You can try this to make unique multidimensional array

$a = topTenGoalGetter();
$input = array_slice(array_values(array_map("unserialize", array_unique(array_map("serialize", $a)))),0,10);
Rahul
  • 18,271
  • 7
  • 41
  • 60