jarvis Posted October 14, 2018 Share Posted October 14, 2018 Hi All, Hoping someone can point out the blinkin obvious! I'm trying to group a bunch of data and total it. For example, the data looks like this: Year: 2018 Value: 10 Year: 2011 Value: 20 Year: 2012 Value: 35 Year: 2013 Value: 35 Year: 2014 Value: 35 Year: 2018 Value: 2 Year: 2015 Value: 4 Year: 2016 Value: 4 Year: 2014 Value: 10 Year: 2015 Value: 15 Year: 2016 Value: 15 Year: 2017 Value: 10 I then use an array to group the data by year, like so: if ($test): $group = array(); foreach ( $test as $t) : echo '<p>Year: '.$t['year'].' Value: '.$t['value'].'</p>'; $group[$t['year']][] = $t['value']; endforeach; endif; This gets me one step further by producing: Array ( [2018] => Array ( [0] => 10 [1] => 2 ) [2011] => Array ( [0] => 20 ) [2012] => Array ( [0] => 35 ) [2013] => Array ( [0] => 35 ) [2014] => Array ( [0] => 35 [1] => 10 ) [2015] => Array ( [0] => 4 [1] => 15 ) [2016] => Array ( [0] => 4 [1] => 15 ) [2017] => Array ( [0] => 10 ) ) What I really need, is to show something like: Array ( [2018] => Array ( [0] => 12 ) [2011] => Array ( [0] => 20 ) [2012] => Array ( [0] => 35 ) [2013] => Array ( [0] => 35 ) [2014] => Array ( [0] => 45 ) [2015] => Array ( [0] => 19 ) [2016] => Array ( [0] => 19 ) [2017] => Array ( [0] => 10 ) ) What would be the best way to achieve this please? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 14, 2018 Share Posted October 14, 2018 (edited) try if ($test): $group = array(); foreach ( $test as $t) : echo '<p>Year: '.$t['year'].' Value: '.$t['value'].'</p>'; if (!isset($group[$t['year']])) { $group[$t['year']] = 0; } $group[$t['year']] += $t['value']; endforeach; echo '<pre>', print_r($group, 1), '</pre>'; endif; EDIT: If your data is originally from a database then a simple query does the whole job for you SELECT year , SUM(val) as total FROM mytable GROUP BY year; Edited October 14, 2018 by Barand 1 Quote Link to comment Share on other sites More sharing options...
jarvis Posted October 14, 2018 Author Share Posted October 14, 2018 Hi @Barand Thank you so much! Just needed to move the print_r outside the loop and it's exactly what I needed. Thanks again! Quote Link to comment Share on other sites More sharing options...
Barand Posted October 14, 2018 Share Posted October 14, 2018 3 minutes ago, jarvis said: Just needed to move the print_r outside the loop and it's exactly what I needed. If you think that was the only change, read my code again. Quote Link to comment Share on other sites More sharing options...
jarvis Posted October 14, 2018 Author Share Posted October 14, 2018 Hi @Barand Oh! But it produces the result I need: Array ( [2018] => 12 [2011] => 20 [2012] => 35 [2013] => 35 [2014] => 45 [2015] => 19 [2016] => 19 [2017] => 10 ) All I need is to work out how to access each one? Or have I totally missed the point? In which case, please accept my apologies! Quote Link to comment Share on other sites More sharing options...
Barand Posted October 14, 2018 Share Posted October 14, 2018 $year = 2014; $totalForYear = $group[$year]; Quote Link to comment Share on other sites More sharing options...
jarvis Posted October 14, 2018 Author Share Posted October 14, 2018 Thanks again @Barand I meant how do I loop through like so: if ($group): foreach ( $group as $g) : echo '<p>Year: '.$g['year'].' Value: '.$g['value'].'</p>'; endforeach; endif; Basically, I'm trying to build up the values so I can loop it out as: Year: 2018 Value: 2 Year: 2011 Value: 20 Year: 2012 Value: 35 Year: 2013 Value: 35 Year: 2014 Value: 10 Year: 2015 Value: 15 Year: 2016 Value: 15 Year: 2017 Value: 10 Thanks again Quote Link to comment Share on other sites More sharing options...
Barand Posted October 14, 2018 Share Posted October 14, 2018 foreach ($group as $year => $total) { echo "Year: $year Total: $total<br>" } Look at your array structure. A foreach() loop uses "... as $key => $value" Quote Link to comment Share on other sites More sharing options...
jarvis Posted October 14, 2018 Author Share Posted October 14, 2018 Hi @Barand, I had: foreach ($group as $k => $v) { echo "\$group[$k] => $v.\n"; } Which looks similar, but I can't get the year to show. I simply get: Year: 0 Total: 10 Year: 1 Total: 12 Year: 2 Total: 19 Year: 3 Total: 19 Year: 4 Total: 20 Year: 5 Total: 35 Year: 6 Total: 35 Year: 7 Total: 45 The key doesn't seem to contain the year, or once again, am I missing the obvious? I truly appreciate your help thought! Quote Link to comment Share on other sites More sharing options...
Barand Posted October 14, 2018 Share Posted October 14, 2018 8 minutes ago, jarvis said: The key doesn't seem to contain the year Yet the print_r of your results that you posted earlier says that it does, namely Array ( [2018] => 12 [2011] => 20 [2012] => 35 [2013] => 35 [2014] => 45 [2015] => 19 [2016] => 19 [2017] => 10 ) The code in your last post does not produce the results in that same post (Why are you escaping the "$"?). How can we help you don't show the code you are using and/or the results you getting from that code. I am going to step away from this now before too much time gets wasted. Quote Link to comment Share on other sites More sharing options...
jarvis Posted October 14, 2018 Author Share Posted October 14, 2018 Ok thanks @Barand Just to say, using your code: foreach ($group as $year => $total) { echo "Year: $year Total: $total<br>"; } Outputs the same as my code: Year: 0 Total: 10 Year: 1 Total: 12 Year: 2 Total: 19 Year: 3 Total: 19 Year: 4 Total: 20 Year: 5 Total: 35 Year: 6 Total: 35 Year: 7 Total: 45 So not sure what you mean? All I'm trying to do is about the above but replacing the Year: 0 with Year 2011 (for example) Quote Link to comment Share on other sites More sharing options...
jarvis Posted October 14, 2018 Author Share Posted October 14, 2018 Hi @Barand, I realised I had sort($group); which was screwing the results, removing this now shows exactly what I needed: Year: 2011 Total: 20 Year: 2012 Total: 35 Year: 2013 Total: 35 Year: 2014 Total: 45 Year: 2015 Total: 19 Year: 2016 Total: 19 Year: 2017 Total: 10 Year: 2018 Total: 12 Thank you once again for all your help Quote Link to comment Share on other sites More sharing options...
Barand Posted October 14, 2018 Share Posted October 14, 2018 To sort by year, use ksort($group). If you want to sort by value you need to preserve the keys, so use asort($group) RTFM ksort() asort() Quote Link to comment Share on other sites More sharing options...
jarvis Posted October 14, 2018 Author Share Posted October 14, 2018 @Barand Once again, many thanks for your time, assistance and patience. It has been very much appreciated!! Quote Link to comment Share on other sites More sharing options...
Barand Posted October 14, 2018 Share Posted October 14, 2018 Patience? ? 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.