jarvis Posted October 26, 2018 Share Posted October 26, 2018 Hi All, I'm at a loss on this and think it's something glaringly obvious. Basically, I've got the following array: rray ( [2016] => Array ( [0] => Array ( [team] => McLaren [value] => 0.4 ) [1] => Array ( [team] => McLaren [value] => 0.4 ) [2] => Array ( [team] => McLaren [value] => 0.4 ) [3] => Array ( [team] => McLaren [value] => 0.4 ) [4] => Array ( [team] => McLaren [value] => 0.4 ) [5] => Array ( [team] => McLaren [value] => 0.4 ) [6] => Array ( [team] => McLaren [value] => 0.4 ) [7] => Array ( [team] => McLaren [value] => 0.4 ) [8] => Array ( [team] => McLaren [value] => 0.4 ) [9] => Array ( [team] => McLaren [value] => 0.4 ) [10] => Array ( [team] => McLaren [value] => 1 ) [11] => Array ( [team] => McLaren [value] => 1 ) [12] => Array ( [team] => McLaren [value] => 1 ) [13] => Array ( [team] => McLaren [value] => 1 ) [14] => Array ( [team] => McLaren [value] => 1 ) [15] => Array ( [team] => McLaren [value] => 2 ) [16] => Array ( [team] => McLaren [value] => 2 ) [17] => Array ( [team] => McLaren [value] => 2 ) [18] => Array ( [team] => McLaren [value] => 2 ) [19] => Array ( [team] => McLaren [value] => 2 ) [20] => Array ( [team] => McLaren [value] => 2 ) [21] => Array ( [team] => McLaren [value] => 2 ) [22] => Array ( [team] => McLaren [value] => 3 ) [23] => Array ( [team] => McLaren [value] => 3 ) [24] => Array ( [team] => McLaren [value] => 10 ) [25] => Array ( [team] => McLaren [value] => 15 ) [26] => Array ( [team] => McLaren [value] => 25 ) [27] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [28] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [29] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [30] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [31] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [32] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [33] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [34] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [35] => Array ( [team] => Red Bull Racing [value] => 0.75 ) [36] => Array ( [team] => Red Bull Racing [value] => 0.75 ) [37] => Array ( [team] => Red Bull Racing [value] => 0.75 ) [38] => Array ( [team] => Red Bull Racing [value] => 1 ) [39] => Array ( [team] => Red Bull Racing [value] => 1 ) [40] => Array ( [team] => Red Bull Racing [value] => 1 ) [41] => Array ( [team] => Red Bull Racing [value] => 1 ) [42] => Array ( [team] => Red Bull Racing [value] => 3 ) [43] => Array ( [team] => Red Bull Racing [value] => 3 ) [44] => Array ( [team] => Red Bull Racing [value] => 5 ) [45] => Array ( [team] => Red Bull Racing [value] => 5 ) [46] => Array ( [team] => Red Bull Racing [value] => 5 ) [47] => Array ( [team] => Red Bull Racing [value] => 7 ) [48] => Array ( [team] => Red Bull Racing [value] => 8 ) [49] => Array ( [team] => Red Bull Racing [value] => 20 ) [50] => Array ( [team] => Red Bull Racing [value] => 30 ) ) [2017] => Array ( [0] => Array ( [team] => McLaren [value] => 0.2 ) [1] => Array ( [team] => McLaren [value] => 0.4 ) [2] => Array ( [team] => McLaren [value] => 0.4 ) [3] => Array ( [team] => McLaren [value] => 0.4 ) [4] => Array ( [team] => McLaren [value] => 0.4 ) [5] => Array ( [team] => McLaren [value] => 0.4 ) [6] => Array ( [team] => McLaren [value] => 0.4 ) [7] => Array ( [team] => McLaren [value] => 0.4 ) [8] => Array ( [team] => McLaren [value] => 0.4 ) [9] => Array ( [team] => McLaren [value] => 0.4 ) [10] => Array ( [team] => McLaren [value] => 0.5 ) [11] => Array ( [team] => McLaren [value] => 1 ) [12] => Array ( [team] => McLaren [value] => 1 ) [13] => Array ( [team] => McLaren [value] => 1 ) [14] => Array ( [team] => McLaren [value] => 1 ) [15] => Array ( [team] => McLaren [value] => 1 ) [16] => Array ( [team] => McLaren [value] => 1 ) [17] => Array ( [team] => McLaren [value] => 1 ) [18] => Array ( [team] => McLaren [value] => 2 ) [19] => Array ( [team] => McLaren [value] => 2 ) [20] => Array ( [team] => McLaren [value] => 3 ) [21] => Array ( [team] => McLaren [value] => 3 ) [22] => Array ( [team] => McLaren [value] => 3 ) [23] => Array ( [team] => McLaren [value] => 3 ) [24] => Array ( [team] => McLaren [value] => 5 ) [25] => Array ( [team] => McLaren [value] => 10 ) [26] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [27] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [28] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [29] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [30] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [31] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [32] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [33] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [34] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [35] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [36] => Array ( [team] => Red Bull Racing [value] => 1 ) [37] => Array ( [team] => Red Bull Racing [value] => 1 ) [38] => Array ( [team] => Red Bull Racing [value] => 1 ) [39] => Array ( [team] => Red Bull Racing [value] => 1 ) [40] => Array ( [team] => Red Bull Racing [value] => 3 ) [41] => Array ( [team] => Red Bull Racing [value] => 3 ) [42] => Array ( [team] => Red Bull Racing [value] => 4 ) [43] => Array ( [team] => Red Bull Racing [value] => 5 ) [44] => Array ( [team] => Red Bull Racing [value] => 7 ) [45] => Array ( [team] => Red Bull Racing [value] => 8 ) [46] => Array ( [team] => Red Bull Racing [value] => 20 ) [47] => Array ( [team] => Red Bull Racing [value] => 30 ) ) [2018] => Array ( [0] => Array ( [team] => McLaren [value] => 0.25 ) [1] => Array ( [team] => McLaren [value] => 0.4 ) [2] => Array ( [team] => McLaren [value] => 0.4 ) [3] => Array ( [team] => McLaren [value] => 0.4 ) [4] => Array ( [team] => McLaren [value] => 0.4 ) [5] => Array ( [team] => McLaren [value] => 0.4 ) [6] => Array ( [team] => McLaren [value] => 0.4 ) [7] => Array ( [team] => McLaren [value] => 0.4 ) [8] => Array ( [team] => McLaren [value] => 0.4 ) [9] => Array ( [team] => McLaren [value] => 0.4 ) [10] => Array ( [team] => McLaren [value] => 0.4 ) [11] => Array ( [team] => McLaren [value] => 0.5 ) [12] => Array ( [team] => McLaren [value] => 0.5 ) [13] => Array ( [team] => McLaren [value] => 1 ) [14] => Array ( [team] => McLaren [value] => 1 ) [15] => Array ( [team] => McLaren [value] => 1 ) [16] => Array ( [team] => McLaren [value] => 1 ) [17] => Array ( [team] => McLaren [value] => 1 ) [18] => Array ( [team] => McLaren [value] => 2 ) [19] => Array ( [team] => McLaren [value] => 2 ) [20] => Array ( [team] => McLaren [value] => 3 ) [21] => Array ( [team] => McLaren [value] => 3 ) [22] => Array ( [team] => McLaren [value] => 3 ) [23] => Array ( [team] => McLaren [value] => 4 ) [24] => Array ( [team] => McLaren [value] => 5 ) [25] => Array ( [team] => McLaren [value] => 5 ) [26] => Array ( [team] => McLaren [value] => 10 ) [27] => Array ( [team] => McLaren [value] => 10 ) [28] => Array ( [team] => Red Bull Racing [value] => 0.2 ) [29] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [30] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [31] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [32] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [33] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [34] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [35] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [36] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [37] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [38] => Array ( [team] => Red Bull Racing [value] => 1 ) [39] => Array ( [team] => Red Bull Racing [value] => 1 ) [40] => Array ( [team] => Red Bull Racing [value] => 1 ) [41] => Array ( [team] => Red Bull Racing [value] => 1 ) [42] => Array ( [team] => Red Bull Racing [value] => 1 ) [43] => Array ( [team] => Red Bull Racing [value] => 3 ) [44] => Array ( [team] => Red Bull Racing [value] => 4 ) [45] => Array ( [team] => Red Bull Racing [value] => 5 ) [46] => Array ( [team] => Red Bull Racing [value] => 7 ) [47] => Array ( [team] => Red Bull Racing [value] => 20 ) [48] => Array ( [team] => Red Bull Racing [value] => 30 ) [49] => Array ( [team] => Red Bull Racing [value] => 40 ) ) ) I then have the following code which outputs a table: $total_value_by_team = array(); foreach ($result as $year => $total) { $runningSum = 0; foreach($total as $key => $value){ if (is_numeric ($value['value'])): $runningSum += $value['value']; else: $runningSum += '0'; endif; $total_value_by_team[] = array( $value['team'], $year, $runningSum); } } $team_names = array(); foreach($total_value_by_team as $record): if(!in_array($record[0], $team_names)) : $team_names[] = $record[0]; endif; endforeach; sort($team_names); #Loop through records to create structured array $outputData = array(); foreach($total_value_by_team as $record): if(!isset($outputData[$record[1]])) : $outputData[$record[1]] = array_fill_keys($team_names, ''); endif; $outputData[$record[1]][$record[0]] = $record[2]; endforeach; #Create headers for table $headers = "<th></th>\n"; foreach($team_names as $team_name): $headers .= "<th>{$team_name}</th>\n"; endforeach; #Create the content for output $output = ''; foreach($outputData as $team => $team_names): $output .= "<tr>\n"; $output .= "<th>{$team}</th>\n"; foreach($team_names as $team_name => $value): $output .= "<td>{$value}</td>\n"; endforeach; $output .= "</tr>\n"; endforeach; This then outputs the following results table: Year | McLaren | Red Bull Racing 2016 | 79 | 175.25 2017 | 42.3 | 131.3 2018 | 57.25 | 175.95 The results for McLaren are correct but the Reb Bull Racing ones are totally out They should be (for 2016 - 2018 respectively). 96.25 89 118.7 It seems to be adding McLaren to Red Bull? Any help is much appreciated! Quote Link to comment Share on other sites More sharing options...
Barand Posted October 26, 2018 Share Posted October 26, 2018 try $results = []; foreach ($data as $yr => $ydata) { if (!isset($results[$yr])) { $results[$yr] = []; } foreach ($ydata as $tdata) { if (!isset($results[$yr][$tdata['team']])) { $results[$yr][$tdata['team']] = 0; } $results[$yr][$tdata['team']] += $tdata['value']; } } echo '<pre>', print_r($results, 1), '</pre>'; where $data is your current array Gives Array ( [2016] => Array ( [McLaren] => 79 [Red Bull Racing] => 96.25 ) [2017] => Array ( [McLaren] => 42.3 [Red Bull Racing] => 89 ) [2018] => Array ( [McLaren] => 57.25 [Red Bull Racing] => 118.7 ) ) BTW, if this data is from a database table it's better to to do it with a query SELECT year , team , SUM(value) as total FROM mytable GROUP BY year, team Quote Link to comment Share on other sites More sharing options...
jarvis Posted October 26, 2018 Author Share Posted October 26, 2018 Hi @Barand That worked a charm! $results = []; foreach ($result as $year => $value) { if (!isset($results[$year])) { $results[$year] = []; } foreach ($value as $value) { if (!isset($results[$year][$value['team']])) { $results[$year][$value['team']] = 0; } $results[$year][$value['team']] += $value['value']; } } echo '<pre>', print_r($results, 1), '</pre>'; May I ask, how would I best convert that to a table output? - Sorry, cheeky I know Quote Link to comment Share on other sites More sharing options...
Barand Posted October 26, 2018 Share Posted October 26, 2018 Added some extra lines to facilitate output (commented) <?php $results = []; $teamnames = []; // array to gather the unique team names foreach ($data as $yr => $ydata) { if (!isset($results[$yr])) { $results[$yr] = []; } foreach ($ydata as $tdata) { if (!isset($results[$yr][$tdata['team']])) { $results[$yr][$tdata['team']] = 0; } $results[$yr][$tdata['team']] += $tdata['value']; $teamnames[$tdata['team']] = 0; // put teamname in array as the key. } } $thead = "<tr><th>Year</th><th>" . join('</th><th>', array_keys($teamnames)) . "</th></tr>\n"; $tdata = ''; foreach ($results as $yr => $tresults) { $totals = $teamnames; // copy of the empty teamnames array foreach ($tresults as $tm => $tot) { $totals[$tm] = $tot; /// put the totals into the team column } $tdata .= "<tr><td>$yr</td><td>" . join('</td><td>', $totals) . "</td></tr>\n"; } ?> <html> <body> <table border='1' style='border-collapse:collapse'> <?=$thead?> <?=$tdata?> </table> </body> </html> 2 Quote Link to comment Share on other sites More sharing options...
jarvis Posted October 27, 2018 Author Share Posted October 27, 2018 @Barand Once again, thank you so much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.