1

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 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?

2
  • 1
    This is known as the 3d bin packing problem. Check the stackoverflow bin-packing tag for similar questions.
    – Olathe
    Apr 20, 2011 at 23:00
  • This problem is called the Bin Packaging Problem (also know as the Knapsack problem). I can't find any php example, so I think it's kinda hard to make something like this...
    – Dnns
    Apr 20, 2011 at 23:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.