Advertisement
Venciity

Soccer Results

Aug 26th, 2014
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.16 KB | None | 0 0
  1. <?php
  2. $input = ["Germany / Argentina: 1-0",
  3.     "Brazil / Netherlands: 0-3",
  4.     "Netherlands / Argentina: 0-0",
  5.     "Brazil / Germany: 1-7",
  6.     "Argentina / Belgium: 1-0",
  7.     "Netherlands / Costa Rica: 0-0",
  8.     "France / Germany: 0-1",
  9.     "Brazil / Colombia: 2-1"];
  10.  
  11. $results = [];
  12.  
  13. foreach ($input as $match) {
  14.     $match = preg_split("/[^a-zA-Z0-9 ]/", $match, -1, PREG_SPLIT_NO_EMPTY);
  15.     $firstTeam = trim($match[0]);
  16.     $secondTeam = trim($match[1]);
  17.     $firstTeamGoals = $match[2];
  18.     $secondTeamGoals = $match[3];
  19.  
  20.     // first team
  21.     if (isset($results[$firstTeam])) {
  22.         $results[$firstTeam]["goalsScored"] += (int) $firstTeamGoals;
  23.         $results[$firstTeam]["goalsConceded"] += (int) $secondTeamGoals;
  24.         array_push($results[$firstTeam]["matchesPlayedWith"] , $secondTeam);
  25.         //$results[$firstTeam]["matchesPlayedWith"] .= ", " . $secondTeam ;
  26.     }
  27.     else {
  28.         $results[$firstTeam]["goalsScored"] = (int) $firstTeamGoals;
  29.         $results[$firstTeam]["goalsConceded"] = (int) $secondTeamGoals;
  30.         $results[$firstTeam]["matchesPlayedWith"] = [] ;
  31.         array_push($results[$firstTeam]["matchesPlayedWith"] , $secondTeam);
  32.     }
  33.  
  34.     //second team
  35.     if (isset($results[$secondTeam])) {
  36.         $results[$secondTeam]["goalsScored"] += (int) $secondTeamGoals;
  37.         $results[$secondTeam]["goalsConceded"] += (int) $firstTeamGoals;
  38.         array_push($results[$secondTeam]["matchesPlayedWith"] , $firstTeam);
  39.         //$results[$secondTeam]["matchesPlayedWith"] .=  ", " . $firstTeam;
  40.     }
  41.     else {
  42.         $results[$secondTeam]["goalsScored"] = (int) $secondTeamGoals;
  43.         $results[$secondTeam]["goalsConceded"] = (int) $firstTeamGoals;
  44.         $results[$secondTeam]["matchesPlayedWith"] = [] ;
  45.         array_push($results[$secondTeam]["matchesPlayedWith"] , $firstTeam);
  46.     }
  47. }
  48. ksort($results);
  49. //var_dump($results);
  50.  
  51. foreach ($results as $match => $value) {
  52.     //echo json_encode($match) . json_encode($value) . "<br>";
  53.     array_multisort($results[$match]['matchesPlayedWith']);
  54. }
  55.  
  56. foreach ($results as $match => $value) {
  57.     echo json_encode($match) . json_encode($value) . "<br>";
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement