Good Day, I'm stump on a loop and was hoping to get some direction. I have 2 arrays, 1 with daily deposits and 1 with daily totals. I need to create a script that will associate the daily totals to the deposits. Sounds easy? Well, the catch all is that there is not a deposit everyday and can actually have a withdrawl from funds if returns were more than the orders.
T0400array(3) {
["11-23-2009"]=>
string(6) "189.73"
["11-25-2009"]=>
string(6) "673.67"
["11-26-2009"]=>
string(6) "158.13"
}

PREarray(7) {
["11-20-2009"]=>
float(-62.75)
["11-21-2009"]=>
string(7) "1016.12"
["11-22-2009"]=>
string(6) "195.37"
["11-23-2009"]=>
float(930.1)
["11-24-2009"]=>
float(-240.04)
["11-25-2009"]=>
string(6) "161.99"
["11-26-2009"]=>
string(6) "171.32"
}

T0400 is the deposits PRE is the daily amounts. I need to create a loop that will add the PRE till it matches the daily deposit so i can choose the deposit date.

Any insights or direction would be appeciated.
THANKS>

    Why use recursion?

    foreach ($pre as $k => $v) {
    	$to[$k] = (isset($to[$k]) ? $to[$k] : 0) + $v;
    }
    

      At times I may need to go back to past dates to add to the deposit total

        Wether by recursion or iteration, everything in your $pre array will be added to the $to array. The method used to achieve this doesn't change the end result, so I really fail to see the point. Recursion adds overhead for function calls (even though they MAY be inlined by a compiler), and needs to allocate more memory.

        # one way of doing it
        function sumElements($to, $add) {
        	if (empty($add))
        		return $to;
        	$k = key($add);
        	$v = array_shift($add);
        	$to[$k] = (isset($to[$k]) ? $to[$k] : 0) + $v;
        	return sumElements($to, $add);
        }
        print_r(sumElements($to, $pre));
        
        #another way of doing it
        function sumElements(&$to, $add) {
        	if (!empty($add)) {
        		$k = key($add);
        		$v = array_shift($add);
        		$to[$k] = (isset($to[$k]) ? $to[$k] : 0) + $v;
        		sumElements($to, $add);
        	}
        }
        sumElements($to, $pre);
        print_r($to);
        

          At this point i'm stuck so i'll give anything a try. I'll see what you got and let you know
          Thanks for everything

            Write a Reply...