Jump to content

Create an array which is based on a given value


Go to solution Solved by Barand,

Recommended Posts

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?

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

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

  • Solution

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
)

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.