Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

[SOLVED] Sum array values in pairs of 4 26 Sep 2012 22:29 #4047

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
Hi

I have a problem that I hope someone can help me a bit with.
foreach ($mortgages as $mortgage_item) {
			$responseArray[$f] = $mortgage_item['mortgage'];
			$f++;
		}

is an array that generates a large array of values, e.g. 60 keys. I need to sum the values in bundles of 4, creating a new array with 15 keys, where key 0 contains the sum of the original array's key 0,1,2,3 - key 1 contains the sum of 4,5,6,7 etc.

But I can't figure this one out, so I hope someone here can help me a bit :-)

Thanks

Please Log in or Create an account to join the conversation.

Last edit: by dyvel.

Re: Sum array values in pairs of 4 27 Sep 2012 06:12 #4049

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
Hi Dyvel,

There are a few ways you could do this...

If the array is always going to be a fixed value - 60 as you said - in my opinion, the easiest option would be to set your $f = 1 before you enter your foreach loop, begin building your new array of sums, iterating over it in the loop until $f = 4, then reset $f = 1.

This way, you should get what your after.

Does that make sense?

You may need to check that your array is divisible by 4 before you begin depending on what you're desired results are too.

Hope it helps,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!

Please Log in or Create an account to join the conversation.

Re: Sum array values in pairs of 4 27 Sep 2012 08:12 #4051

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
Hi

Thank you for your reply and time.
It is not a fixed value, and based on how many years a loan is running. The value 4 could also be 2 or 12

Please Log in or Create an account to join the conversation.

Re: Sum array values in pairs of 4 27 Sep 2012 09:14 #4055

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
This is giving me to much grey hair :-D

Please Log in or Create an account to join the conversation.

Re: Sum array values in pairs of 4 27 Sep 2012 09:21 #4057

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
It reminds me the mathematicals problems at school.

Once I have a bit of time I want to do it for fun.

It is a long time I havn't done mathematics in programmation. I suddenly feel nostalgic of my first 3D engine under Qbasic. lol.
Coding is now a piece of cake

Please Log in or Create an account to join the conversation.

Re: Sum array values in pairs of 4 27 Sep 2012 09:25 #4058

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
I'm getting there... array_chunk seems to be the road to follow :-)
$responseArray = array();
		
		$f = 0;		
	
		foreach ($mortgages as $mortgage_item) {
			
			$responseArray[$f] = $mortgage_item['mortgage'];
			
			$f++;
		}
		
		return array_chunk($responseArray,$payment_frequency);

is giving me a multidimensional array like
Array
(
    [0] => Array
        (
            [0] => 10850
            [1] => 10850
            [2] => 10850
            [3] => 10850
        )

    [1] => Array
        (
            [0] => 10850
            [1] => 10850
            [2] => 10850
            [3] => 10850
        )
}

Now I "just" need to sum the values

Please Log in or Create an account to join the conversation.

Re: Sum array values in pairs of 4 27 Sep 2012 09:39 #4060

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
Hum, not really advanced with that.
I do not think this function array_chunk is usefull for you.

I would have say, a double loop.

Algorithm :
NbCalculations = NbMortgages - frequency
Values = [];
for(i=0, i < count(NbCalculations), i++)
  Sum = 0
  for(j = 0, j < frequency, j++)
      Sum += Mortgages[i + j];
  endfor

  Values[i] = Sum
endfor

The first loop is sliding the first index (0, 1, 2)
The second loop is summing the values after it.

I am not sure if I understood well.
Coding is now a piece of cake

Please Log in or Create an account to join the conversation.

Last edit: by admin.

Re: Sum array values in pairs of 4 27 Sep 2012 09:39 #4061

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
I did it :woohoo:
		$responseArray = array();
		$arrayChunk = array();
		$mergeArray = array();
		
		$f = 0;		
	
		foreach ($mortgages as $mortgage_item) {
			
			$responseArray[$f] = $mortgage_item['mortgage'];
			
			$f++;
		}
		
		$arrayChunk = array_chunk($responseArray,$payment_frequency);
		
		for ( $i = 0 , $n = count($arrayChunk) ; $i < $n ; $i++ ) :
			 
			 $mergeArray[$i] = array_sum($arrayChunk[$i]);	
			 
		endfor;
		
		return $mergeArray;

There might be a better solution to it, but it works

It returns an array like
Array
(
    [0] => 43400
    [1] => 43400
    [2] => 43400
    [3] => 43400
    [4] => 43400
    [5] => 132177.28
    [6] => 132177.28
    [7] => 132177.28
    [8] => 132177.28
    [9] => 132177.28
    [10] => 132177.28
    [11] => 132177.28
    [12] => 132177.28
    [13] => 132177.28
    [14] => 132177.43
)

Please Log in or Create an account to join the conversation.

Last edit: by dyvel.

Re: Sum array values in pairs of 4 27 Sep 2012 09:42 #4062

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11

admin wrote: Hum, not really advanced with that.
I do not think this function array_chunk is usefull for you.

I would have say, a double loop.

Algorithm :

NbCalculations = NbMortgages - frequency
Values = [];
for(i=0, i < count(NbCalculations), i++)
  Sum = 0
  for(j = 0, j < frequency, j++)
      Sum += Mortgages[i + j];
  endfor

  Values[i] = Sum
endfor

The first loop is sliding the first index (0, 1, 2)
The second loop is summing the values after it.

I am not sure if I understood well.


This looks like a more elegant solution than what I could come up with... will try if I can get it to work

Please Log in or Create an account to join the conversation.

Re: Sum array values in pairs of 4 27 Sep 2012 09:53 #4063

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
After, I read again, and I think it is not giving the result you want.
If you want to jump every 'frequency' records, so change in :

NbCalculations = (ceil)(NbMortgages / frequency)

and :

Sum += Mortgages[i*frequency + j];

I think...

Good luck
Coding is now a piece of cake
The following user(s) said Thank You: dyvel

Please Log in or Create an account to join the conversation.

Re: Sum array values in pairs of 4 27 Sep 2012 12:21 #4074

You have 15 variables in the array but you need to add in multiples of 4 that means you need to move to the next record when you have read the 15th value double loop is needed
below in the format record-arrayposition 1-2 = record 1 position2
[1-1 to 1-4]
[1-5 to 1-8]
[1-9 to 1-12]
[1-13 to 1-15] plus [2-1]

[2-2 to 2-5]
[2-6 to 2-9]
[2-10 to 2-13]
[2-14 to 2-15] plus [3-1 to 3-2]

Sorry looked at what you needed again and was off track with the above

Please Log in or Create an account to join the conversation.

Last edit: by BTB300.

Re: Sum array values in pairs of 4 27 Sep 2012 12:23 #4076

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
Thanks, but as you can see from my previous post, I got it solved

Please Log in or Create an account to join the conversation.

Last edit: by dyvel.

Re: Sum array values in pairs of 4 27 Sep 2012 12:27 #4077

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986

give me 5 i will construct for you


MDR !! :lol:
Too late...
Coding is now a piece of cake

Please Log in or Create an account to join the conversation.

Re: Sum array values in pairs of 4 27 Sep 2012 13:50 #4094

To Late
its been a while since i did something like that
:whistle:

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.088 seconds

The j-cook project is one of the best of its kind and it is nice that we all try to contribute in little ways to make life easy for each other.

vlemos (Forum)  

Get Started