1

I'm trying to get the player with the highest rating from the following JSON object. I have two teams home and away and I want to compare all ratings from all players and return the highest rating value and the name of the player.

   {
        "lineup": {
            "home": {
                "starters": [{
                        "name": "Andreas",
                        "statistics": {
                            "rating": 6.38
                        }
                    },
                    {
                        "name": "Carlos",
                        "statistics": {
                            "rating": 6.87
                        }
                    }
                ]
            },
            "away": {
                "starters": [{
                        "name": "Felix",
                        "statistics": {
                            "rating": 7.20
                        }
                    },
                    {
                        "name": "Daniel",
                        "statistics": {
                            "rating": 4.87
                        }
                    }
                ]
            }
        }
    }

Keep in mind that my JSON includes 30 players with their ratings and not 4

What I've tried so far.

Attempt #1:

I've tried to get the max from home and away team and then compare these two values and get the highest, for some reason it doesn't return the maximum value on each team. Ex for team home returns the player with the rating 6.38 instead the other one.

//Home
$max = max($decode_one['lineup']['home']['starters']);
$finalVal = $max['statistics']['rating']; 

//Away
$max1 = max($decode_one['lineup']['away']['starters']);
$finalVal1 = $max1['statistics']['rating'];

Attempt #2:

Here I've added the ratings inside to a new array and then with a loop got the max value from the array. The 2 issues that I have is first the JSON includes 30 players, 15 from home and 15 away for some reason it puts only the 15 players from home and not from both. I think that is because the keys from each team are the same (0-14) and the other issue I want as well to return name of the selected player.

$result = array();
foreach ($decode_one['lineup'] as $homeOrAway) {
    foreach ($homeOrAway as $startersOrSubs) {
        foreach ($startersOrSubs as $key => $value) {
            $result[$key['rating']][]  = $value['statistics']['rating'];            
        }
    }
}

foreach ($result as $key => $maxValue) {
    echo "{$key}: " . max($maxValue) . "\n";
}

Any ideas?

Thank you

trunks
  • 147
  • 2
  • 9
  • [No such thing as a JSON object](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – KooiInc Jun 09 '20 at 07:45
  • Calling `max` directly on those `starters` arrays makes no sense - you do not have simple numeric values in there, you have objects. – CBroe Jun 09 '20 at 07:45
  • try looking here: https://stackoverflow.com/questions/4020796/finding-the-max-value-of-an-attribute-in-an-array-of-objects – Nobady Jun 09 '20 at 07:46
  • This will get you object having highest rating `Object.values(obj.lineup).flatMap(k=>k.starters).sort((a,b)=>b.statistics.rating-a.statistics.rating)[0]` – gorak Jun 09 '20 at 07:50

2 Answers2

2

PHP version, using array_reduce to find the maximum element in the combined array of players from both “home” and “away”:

$max = array_reduce(
  array_merge(
    $decode_one['lineup']['home']['starters'],
    $decode_one['lineup']['away']['starters']
  ),
  function($carry, $item) {
    if( $carry === NULL) {
      return $item;
    }
    else {
      return $carry['statistics']['rating'] > $item['statistics']['rating'] ?
        $carry : $item;
    }
  }
);
CBroe
  • 91,630
  • 14
  • 92
  • 150
  • Thanks for the PHP solution it works as I wanted. I'm facing a small issue, sometimes some players don't have a rating value and it returns me the error `Undefined index: rating`. I've tried to check if there is `['statistics']['rating']` without success. – trunks Jun 09 '20 at 20:56
0

// A JS solution

getHighestRating = obj => {
    let starters = obj.lineup.home.starters.concat(obj.lineup.away.starters), highestRating = 0;
    for(const player of starters){
        highestRating = highestRating < player.statistics.rating ? player.statistics.rating : highestRating;
    }
    return highestRating;
}
getHighestRating(obj);

// Hope this will help
Subhadeep
  • 149
  • 5