Desperate For Array Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Desperate For Array Help

Post 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!
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: Desperate For Array Help

Post 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.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Desperate For Array Help

Post 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.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Desperate For Array Help

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Desperate For Array Help

Post 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;
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Desperate For Array Help

Post by JakeJ »

pickle wrote:Something like this?
I will give it a try and see what what happens. Muchas gracias!
Post Reply