PHP calculations in an array (recursive?)
<p>I'm trying to write a function that calculates the grand total price of each order, when given the price of the product and a credit that this user has. The credit should be deducted and if anything remains it should be deducted from the next order.</p>

<p>The function should return the final price of each order and the credits that remain foreach each account in <strong>one single array</strong> with the customer IDs as keys. Any negative numbers (credit or order prices) should all be set to 0.</p>

<p>'<code>abc</code>' and '<code>def</code>' are customer IDs. This sample code should explain things much better. <strong>Do I need a recursive function for this?</strong></p>

<p>Input: </p>

<pre><code>//should return: order 1 = 375, order 2 = 90, remaining credit = 0;
$order['abc'][1] = 500;
$order['abc'][2] = 90;
$credit['abc'] = 125;

//should return: order 1: 0, order 2: 0, remaining credit = 125
$order['def'][1] = 100;
$order['def'][2] = 75;
$credits['def'] = 300;
</code></pre>

<p>The return should be one single array, as such:</p>

<pre><code>$set['abc'][1] = 375;
$set['abc'][2] = 90;
$set['abc']['credit_remaining'] = 0;
$set['def'][1] = 0;
$set['def'][2] = 0;
$set['def']['credit_remaining'] = 125;
</code></pre>
