Sort products in an array into packages
I'm trying to write a script that sort products into packages. I have an array with products ($products) that contains length, width, height and weight. There are three different packages and the conditions will look like this
small: length <= 17, width <= 14, height <= 4, weight <= 2
medium: length <= 26, width <= 18, height <= 4, weight <= 2
large: length <= 35, width <= 23, height <= 4, weight <= 2
So now the script needs to check if small is enough for all the products, if not then medium, if not then large. If large开发者_StackOverflow中文版 is not enough, then I want the script to check which combinations of the products fits the large package closest, then remove those elements and keep checking with the rest of the elements in the array and fit them into appropriate packages. In short: it should sort the products into as large packages as possible without wasting space.
I've started with making if statements to see if the total length and width of the products in the order fits into the different sizes, but the difficult part is finding the best combination of products for a package and removing them from the array to keep sorting the rest.
Any ideas?
精彩评论