I have an array of part lengths, for examples sake:-
array(150, 180, 270);
I then have a measurement ($a = 440)
I need to calculate the two closest possible combinations of lengths which are greater than $a without manually having to write hundreds of possible combinations in order to work it out.
So:
150
180
270
150 + 150
150 + 180
150 + 270
180 + 180
180 + 270
270 + 270
150 + 150 + 150
150 + 150 + 180
..and so on.
This will need to run for a set number of times, rather than just finding the first two matches and stopping, as 150 + 150 + 150 would be a closer match to $a than 270 + 270 but may run after.
edit: I also need to store the combination of parts which made up the match, preferably in an array.
I hope I’ve explained this well enough for somebody to understand.
As this is quite a resource heavy script, I thought it would be a good idea to give the option to generate the choices beforehand, then use that data to create a variable/object/sql script to permanently store the data. For instance, doing something like
The new script I have is similar, but it just generates an array of all combinations without any duplicates. Seems pretty quick again. Notice the $maxLength variable, which is currently set to 2000, which can be modified with your own largest possible size.