Using php5.2 and MySQL 4.1.22
I came across something that at first seemed simple, but has since deviated from me regarding a simple, clean solution.
We have predefined product packages. Package 1 may contain products A, B, and C. Package 2 may have A, C, D, and G therein, etc. Packages have a size of 3 to 5 products.
Now the client can choose any 10 available products and make a "custom". Since we already have certain predefined packages, we would like to create a custom package with smaller existing packages (for ease of delivery) where possible.
So, for example, the client chooses to create a “custom package” of products A, B, C, D, E, and F. We already have a predefined package containing A, B, and C called Foo. So the order will then be Foo, D, E, and F.
The catch has the least number of individual elements, followed by the least number of packets. For instance:
Custom Package: A, B, C, D, E, F, G, H, I, J.
Predefined Package (1): A, B, C, D, E
Predefined Package (2): A, B, C
Predefined Package (3): D, E, F
If I just take the greatest match, then I have 1 (5pc) packet and 5 separate elements. No packages (2) and (3) can be built with the rest of the elements.
If I look deeper, I find that without creating a package (1), I can instead create a package (2) and a package (3). This means that I have 2 packages and 4 separate elements (the best choice in this buisiness rule).
Since I use MySQL, I hold back only one level of sub-block selection (as far as I know). So this view should be done in php. I looked at using array_intersect () to match, but every factor found exponentially grows with respect to processing, as the number of predefined packets grows linearly.
I ran this a couple of other coder friends and again, while it seemed like there should be a simple answer, which we all found out that it was not as simple as it seems. So, I thought I'd post it here like a good noodle stretcher. Thanks so much for your time!