Page 1 of 1

Desperate For Array Help

Posted: Thu Dec 10, 2009 7:08 pm
by JakeJ
I have a problem that I hope isn't too unusual. There's a good chance that I'm just approaching the whole thing from the wrong angle. So I'll describe the whole thing.

My array is this:

1. I have a function called nper() that calculates the number of payments left on a debt. It mirrors the function of the same name in MS Excel. I actually have to calculate nper twice (before and after comparison). I store the results of the first nper calculation (along with some other data) in an array and sort nper ascending.
2. I have a variable called $margin which is an additional amount applied ONLY to the debt with the lowest nper() result. The other debts in the array will have their balances reduced by ($nper * $minpay).
3. Before calculating nper() on the second debt, $margin = $margin + $minpay(debt1). Then $nper = $nper + $nper(debt1). The reason for this is that I need a running total of how many months it takes to pay off each debt.
The end result I really need is $nper for the last debt.

Since there are x number of debts, I obviously have to loop through this. Here's my problem:

I cannot figure out a way to loop through the array and get the results I want.

debt1 = $nper(apr, pmt, pv, margin) but the remaining debts get $balance = $balance - $minpay.

As I said, I'm probably just going about this the wrong way and my programming skills are still on the basic side.

Thanks!

Re: Desperate For Array Help

Posted: Thu Dec 10, 2009 8:38 pm
by Griven
Please post some code to illustrate your description. Without seeing how you are currently going about it, there's little we can do to help you.

Re: Desperate For Array Help

Posted: Thu Dec 10, 2009 9:44 pm
by JakeJ
Griven wrote:Please post some code to illustrate your description.
The problem is, I haven't been able to figure out even sample code yet.

Maybe I should distill it down to this:

With a multidimensional array, I need to be able to do something with the first record and something else with the rest of the records.

After that, I need to skip the first record that was processed before and repeat the process starting with the second record.

record1 - functiona()
additional records - functionb()
skip record1
record2 - functin(a)
additional records - functionb()
skip record2 ... etc.

I thought also thought about:

While ($i > 0) {
functiona();
Foreach($z) {
next($array);
functionb();
}
$i--;
}

The problem with that is that I need to skip the first iteration and start with the second one and continue in the same pattern.

Re: Desperate For Array Help

Posted: Thu Dec 10, 2009 10:44 pm
by JakeJ
I came up with a partial solution:

while($row = mysql_fetch_array($nresult)) {
$nper = nper($row['apr']/12/100, -($row['min_pay'] + $margin), $row['starting_balance']);
Echo "NPER :", $nper, " Creditor: ",$row['creditor_name'], " Balance: $",$row['starting_balance'],"<br>";

next($row);
$row['starting_balance'] = $row['starting_balance'] - $row['min_pay'];
Echo "Creditor: ",$row['creditor_name'], " Balance: $",$row['starting_balance'],"<br>";
}

Now the challenge is this:

When it loops back to the first record, it has to skip it (filter?) and do with the second record what it did with the first record on the first pass until it's gone through all of the records that way.

Also.. if there are 5 debts I need to perform next($row) 4 times. I'm getting there. Slow but sure.

Re: Desperate For Array Help

Posted: Fri Dec 11, 2009 2:42 pm
by pickle
Something like this?

Code: Select all

$calculated_values = array();
 
//do stuff to the first element
$first_element = mysql_fetch_array($result);
$calculated_values[0] = //result of work on $first_element
 
//Do stuff to 1+n elements
while($row = mysql_fetch_array($result))
{
  $calculated_values[] = //result of working on $row
}
 
//Do stuff to 2+n elements
for($i = 2;$i++;$i<count($calculated_values))
{
  $current_value = $calculated_values[$i];
 
  //do stuff to $current_value;
 
  $calculated_values[$i] = $current_value;
}

Re: Desperate For Array Help

Posted: Fri Dec 11, 2009 9:19 pm
by JakeJ
pickle wrote:Something like this?
I will give it a try and see what what happens. Muchas gracias!