I have a hard problem and I cannot fully solve it using PHP and/or Mysql.I have two arrays payments and invoices per user.
Some assumptions are:
- Payment and invoice totals match, for each user
- Payments can be 0
There are at least 3 scenarios:
- Payments and invoices match one to one, same number same value
- Payments number is greaten than invoices number, so one payment can match one to one other payment could be the sum of two or more invoice
- Invoices number is greaten than payments number, so one payment can match one to one other invoice could be the sum of two or more payments
Other scenario could be a variation of the first or second, where there are some payments with value 0 so the real scenario would be the first of the third
I need as result another array with key equals to invoice key and value the keys of payments comma separated (if more than one per invoice) in the third scenario two or more keys could have the same value
First scenario is really simple, second scenario is not perfect because if there are zero value it’s possible to do not have a complete match, the third scenario is a mess.
Someone have an idea? Elaboration Time is not a problem, it’s done offline
Below an example for first scenario
$payments = [1,2,3];
$invoices = [1,3,2];
$result = [0 => 0, 1 => 2, 2 => 1];
second scenario
$payments = [15,20,0,30,40];
$invoices = [55,50];
$result = [0 => "0,2,3", 1 => "1,4"];
or
$result = [0 => "0,3", 1 => "1,4"];
third scenario
$payments = [15,20,30,40];
$invoices = [10,5,20,17,13,36,3,1];
$result = [0 => "0", 1 => "0", 2 => "1", 3 => "2", 4 => "2", 5 => "3", 6 => "3", 7 => "3"];
2