Advertisement
Venciity

Soccer Results by Alex

Aug 28th, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 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.  
  35.     //second team
  36.     if (isset($results[$secondTeam])) {
  37.         $results[$secondTeam]["goalsScored"] += (int) $secondTeamGoals;
  38.         $results[$secondTeam]["goalsConceded"] += (int) $firstTeamGoals;
  39.         array_push($results[$secondTeam]["matchesPlayedWith"] , $firstTeam);
  40.         //$results[$secondTeam]["matchesPlayedWith"] .=  ", " . $firstTeam;
  41.     }
  42.     else {
  43.         $results[$secondTeam]["goalsScored"] = (int) $secondTeamGoals;
  44.         $results[$secondTeam]["goalsConceded"] = (int) $firstTeamGoals;
  45.         $results[$secondTeam]["matchesPlayedWith"] = [] ;
  46.         array_push($results[$secondTeam]["matchesPlayedWith"] , $firstTeam);
  47.  
  48.     }
  49. }
  50. ksort($results);
  51.  
  52. foreach ($results as $key => $value) {
  53.     foreach ($results[$key] as $inner => $stoynost) {
  54.         if ($inner === "matchesPlayedWith") {
  55.             sort($results[$key][$inner]);
  56.         }
  57.     }
  58. }
  59. echo json_encode($results);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement