Reputation: 612
I have this array with teams that are in a competition, now i want to do some calculations for each match that is going to happen.
Array
(
[Team1] => Array
(
[totalHomeGoals] => 48
[totalHomeAgainstGoals] => 12
[totalAwayGoals] => 31
[totalAwayAgainstGoals] => 11
)
[Team2] => Array
(
[totalHomeGoals] => 31
[totalHomeAgainstGoals] => 25
[totalAwayGoals] => 25
[totalAwayAgainstGoals] => 27
)
[Team3] => Array
(
[totalHomeGoals] => 22
[totalHomeAgainstGoals] => 21
[totalAwayGoals] => 15
[totalAwayAgainstGoals] => 38
)
)
So now i want to make calculations for each possible match. To make it simple let's say i want to add the AwayGoals to the Homegoals, so like this:
Team1 vs Team2 -> 48 + 25
Team2 vs Team1 -> 31 + 31
Team1 vs Team3 -> 48 + 15
Team3 vs Team1 -> 22 + 31
Team2 vs Team3 -> 31 + 15
etc
etc for all 12 possible matches
What direction should i look to accomplish this?
Upvotes: 0
Views: 75
Reputation: 4379
Ok here is my proposal.
First I fixed your array.
$teams = Array
(
'Team1' => Array
(
'totalHomeGoals' => 48,
'totalHomeAgainstGoals' => 12,
'totalAwayGoals' => 31,
'totalAwayAgainstGoals' => 11
),
'Team2' => Array
(
'totalHomeGoals' => 31,
'totalHomeAgainstGoals' => 25,
'totalAwayGoals' => 25,
'totalAwayAgainstGoals' => 27
),
'Team3' => Array
(
'totalHomeGoals' => 22,
'totalHomeAgainstGoals' => 21,
'totalAwayGoals' => 15,
'totalAwayAgainstGoals' => 38,
)
);
Then I created a function that will provide unique team result and not. Here is what the code looks like.
function showResult($i, $j, $param1, $param2, $teams, $teamKeys) {
echo $teamKeys[$i] . ' vs ' . $teamKeys[$j] . ' -> ' . $teams[$teamKeys[$i]][$param1] . ' + ' . $teams[$teamKeys[$j]][$param2] . '<br/>';
}
function getAllResult($teams, $param1, $param2, $unique=true) {
$teamKeys = array_keys($teams);
for ($i = 0; $i < count($teams); $i++) {
if(!$unique) {
for ($j = 0; $j < count($teams); $j++) {
showResult($i, $j, $param1, $param2, $teams, $teamKeys);
}
} else {
for ($j = $i + 1; $j < count($teams); $j++) {
showResult($i, $j, $param1, $param2, $teams, $teamKeys);
}
}
}
}
Here is how it works if you want to repeat the probability, such as Team1 VS Team2 and Team2 VS Team1.
getAllResult($teams, 'totalHomeGoals', 'totalAwayGoals', false);
Output:
Team1 vs Team1 -> 48 + 31
Team1 vs Team2 -> 48 + 25
Team1 vs Team3 -> 48 + 15
Team2 vs Team1 -> 31 + 31
Team2 vs Team2 -> 31 + 25
Team2 vs Team3 -> 31 + 15
Team3 vs Team1 -> 22 + 31
Team3 vs Team2 -> 22 + 25
Team3 vs Team3 -> 22 + 15
Here is how it works if you want to have only unique probabilities.
getAllResult($teams, 'totalHomeGoals', 'totalAwayGoals');
Output:
Team1 vs Team2 -> 48 + 25
Team1 vs Team3 -> 48 + 15
Team2 vs Team3 -> 31 + 15
Then you can change the attribute for whatever you would prefer.
Let me know if you have any question
Upvotes: 1