thara Posted September 27, 2021 Share Posted September 27, 2021 Hi, My expectation is to get an array which is based on a given value. In my POS system, I do have an array that shows all the baskets that still have an outstanding balance to be due by the customer for each basket. The array looks like this: Array ( [15] => 1088.00 [13] => 2206.00 [11] => 1716.00 [9] => 1210.00 [7] => 140.00 ) key = basket_id value = balance due The customer can take the sum of the amount due and make payments in one lump sum or in installments without paying separately for each baskets. My question is, Depending on the amount the customer pays, I need to create a new array. Suppose the amount paid by the customer is 5000, then the new array should be as follows. Array ( [15] => 1088.00 [13] => 2206.00 [11] => 1706.00 ) In this newly created array, array_sum() should be equal to the amount paid by customer. (According to this particular example it should be 5000) Can I know the better workaround for this? Quote Link to comment https://forums.phpfreaks.com/topic/313824-create-an-array-which-is-based-on-a-given-value/ Share on other sites More sharing options...
ginerjm Posted September 27, 2021 Share Posted September 27, 2021 What are you doing with the payments? IF the user paid 5000 why is the array still equal to 5000? Shouldn't it be the remainder of the 5000? I don't understand why you want this array to be created. Quote Link to comment https://forums.phpfreaks.com/topic/313824-create-an-array-which-is-based-on-a-given-value/#findComment-1590409 Share on other sites More sharing options...
Barand Posted September 27, 2021 Share Posted September 27, 2021 That seems counter-intuitive to me. Startting with your original array, I would expect the remaining array, after customer pays 5000, to be 15 => 1088 13 => 272 where the 5000 clears the balance from the three oldest basket amounts (7, 9, 11) and leaves 272 due on basket 13 Quote Link to comment https://forums.phpfreaks.com/topic/313824-create-an-array-which-is-based-on-a-given-value/#findComment-1590410 Share on other sites More sharing options...
thara Posted September 27, 2021 Author Share Posted September 27, 2021 12 minutes ago, Barand said: where the 5000 clears the balance from the three oldest basket amounts (7, 9, 11) Thanks for mentioned it. new array should be in this order. Array ( [13] => 1934.00 [11] => 1716.00 [9] => 1210.00 [7] => 140.00 ) The amout should be clear the balance from oldest baskets Quote Link to comment https://forums.phpfreaks.com/topic/313824-create-an-array-which-is-based-on-a-given-value/#findComment-1590414 Share on other sites More sharing options...
Solution Barand Posted September 27, 2021 Solution Share Posted September 27, 2021 Ah! Two arrays $amts_due = [ '15' => 1088.00, '13' => 2206.00, '11' => 1716.00, '9' => 1210.00, '7' => 140.00 ]; $amts_paid = []; $payment = 5000.00; ksort($amts_due); // oldest first foreach ($amts_due as $bid => &$due) { if ($payment >= $due) { $payment -= $due; $amts_paid[$bid] = $due; $due = 0; } else { $due -= $payment; $amts_paid[$bid] = $payment; $payment = 0; } } $amts_due = array_filter($amts_due); $amts_paid = array_filter($amts_paid); Giving Due Array ( [13] => 272 [15] => 1088 ) Paid Array ( [7] => 140 [9] => 1210 [11] => 1716 [13] => 1934 ) Quote Link to comment https://forums.phpfreaks.com/topic/313824-create-an-array-which-is-based-on-a-given-value/#findComment-1590420 Share on other sites More sharing options...
thara Posted September 28, 2021 Author Share Posted September 28, 2021 18 hours ago, Barand said: Ah! Two arrays $amts_due = [ '15' => 1088.00, '13' => 2206.00, '11' => 1716.00, '9' => 1210.00, '7' => 140.00 ]; $amts_paid = []; $payment = 5000.00; ksort($amts_due); // oldest first foreach ($amts_due as $bid => &$due) { if ($payment >= $due) { $payment -= $due; $amts_paid[$bid] = $due; $due = 0; } else { $due -= $payment; $amts_paid[$bid] = $payment; $payment = 0; } } $amts_due = array_filter($amts_due); $amts_paid = array_filter($amts_paid); Giving Due Array ( [13] => 272 [15] => 1088 ) Paid Array ( [7] => 140 [9] => 1210 [11] => 1716 [13] => 1934 ) Thank you so much... Its sloved my problem. Quote Link to comment https://forums.phpfreaks.com/topic/313824-create-an-array-which-is-based-on-a-given-value/#findComment-1590462 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.