AlexKondov

Soccer Scores

Aug 26th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 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.     $results = [];
  11.  
  12.     foreach ($input as $match) {
  13.         $match = preg_split("/[\/:-]+/", $match);
  14.         if (isset($results[$match[0]])) {
  15.             $results[$match[0]]['Matches  Against'] .= $match[1];
  16.             $results[$match[0]]["Goals"] += (int)$match[2];
  17.             $results[$match[0]]["Goals Against"] += (int)$match[3];
  18.         }
  19.         else {
  20.             $results[$match[0]]['Matches  Against'] = $match[1];
  21.             $results[$match[0]]["Goals"] = (int)$match[2];
  22.             $results[$match[0]]["Goals Against"] = (int)$match[3];
  23.         }
  24.     }
  25.     ksort($results);
  26.     foreach ($results as $match => $value) {
  27.         echo json_encode($match) . json_encode($value) . "<br>";
  28.     }
  29. ?>
Advertisement
Add Comment
Please, Sign In to add comment