I have an array that represents soccer match “results” (team1 – the name represented by a number and team1_points that represents how many points this team earn from this match (3 for win, 1 for draw and 0 for lost).
Here is the code:
array (size=9)
0 =>
array (size=2)
'team1' => string '2' (length=1)
'team1_points' => string '0' (length=1)
1 =>
array (size=2)
'team1' => string '2' (length=1)
'team1_points' => string '0' (length=1)
2 =>
array (size=2)
'team1' => string '2' (length=1)
'team1_points' => string '0' (length=1)
3 =>
array (size=2)
'team1' => string '14' (length=2)
'team1_points' => string '0' (length=1)
4 =>
array (size=2)
'team1' => string '14' (length=2)
'team1_points' => string '3' (length=1)
5 =>
array (size=2)
'team1' => string '1' (length=1)
'team1_points' => string '3' (length=1)
6 =>
array (size=2)
'team1' => string '13' (length=2)
'team1_points' => string '1' (length=1)
7 =>
array (size=2)
'team1' => string '5' (length=1)
'team1_points' => string '1' (length=1)
8 =>
array (size=2)
'team1' => string '7' (length=1)
'team1_points' => string '0' (length=1)
As you can see some of the team1 are repeating like ‘team1’ => ‘2’ is there 3 times and team with id=”14″ is there 2 times ‘team1′ => ’14’ etc.
I need to group the same teams based on team1 column (teams with the same id e.g. 2 or 14 etc. and create a new alias column called e.g. team_1_occurrences tht will hold the number of that team in the array and also a column e.g. team1_total_points that will SUM all the team1_points (3,1 or 0).
And then order by team1_total_points column DESC.
So, in the end I need something like this:
team1 | team1_occurrences | team1_total_points
----------------------------------------------
14 | 2 | 3
----------------------------------------------
2 | 3 | 0
…
My array created from while loop contains only 2 columns team1 and team1_points (the result is the var_dump in the beginning of my question:
$matches_array[] = array(
'team1'=> $team_1_id,
'team1_points'=> $team1_points
);
I guess I need somehow use the php function array_count_values and create a new column but I don’t know how.
Thanks in advance for any advice how to solve this.
You can try
Output