Jump to content

Group and Total


jarvis

Recommended Posts

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?

Link to comment
Share on other sites

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 by Barand
  • Great Answer 1
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.