Location>code7788 >text

Common specification permutation problems

Popularity:952 ℃/2024-11-20 17:12:32

The most common in doing mall system is the specification, abbreviation pcs. Specifically, a commodity has more than one attribute, each attribute has more than one specification, so that the formation of some permutations and combinations, do the commodity inventory when we have to set these combinations of inventory and price.

For example, a computer memory 16G, 32G and 64G, hard disk 500G and 1T, graphics card integrated graphics and discrete graphics card, such goods in the sale of selected different specifications price is not the same, the warehouse stocking inventory is of course not the same.

Then the background of the mall to set the price of these products individually, you need to arrange all the combinations to set the price.

Now we know that we should generate such permutations and then set the price and inventory (The prices and inventories here are just an example for the program, not the actual market prices.

random access memory (RAM) hard disk display card (computer) prices property or cash held in reserve
16G 500G integrated (as in integrated circuit) 5000 100
16G 500G stand alone 6000 200
16G 1T integrated (as in integrated circuit) 7000 220
16G 1T stand alone 6500 120
32G 500G integrated (as in integrated circuit) 7000 110
32G 500G stand alone 7500 200
32G 1T integrated (as in integrated circuit) 7500 300
32G 1T stand alone 8000 200
64G 500G integrated (as in integrated circuit) 7500 134
64G 500G stand alone 8000 347
64G 1T integrated (as in integrated circuit) 8500 258
64G 1T stand alone 9000 35

Here are a total of 12 combinations, so how do you programmatically generate such combinations?

The attribute variables that we know we can get by reading from the database are as follows:

<?php
$spec_list = [
    'memory' => ['16G', '32G', '64G'],
    'storage' => ['500G', '1T'],
    'graphics' => ['integrated', 'discrete'],
];

What we want is definitely an array with 12 elements in it, and in each element is a combination of those specifications.

Something like this result

[
  ['16G', '500G', 'Integration'],
  ['16G', '500G', 'Independent'],
  ['16G', '1T', 'Integration'],
  ['16G', '1T', 'Independent'],
  ['32G', '500G', 'Integration'],
  ['32G', '500G', 'Independent'],
  ['32G', '1T', 'Integration'],
  ['32G', '1T', 'Independent'],
  ['64G', '500G', 'Integration'],
  ['64G', '500G', 'Independent'],
  ['64G', '1T', 'Integration'],
  ['64G', '1T', 'Independent'],
]

So how do you do it? If you write a three-level loop, nested implementation, that's no problem, that is, the number of attributes you have to clearly know how many kinds, this is after all not a permanent solution, and attributes may be increased or decreased, then the background of the future increase in the attributes, the program how to do?

Still have to figure out all combinations automatically, no need to manually detect how many properties there are to foreach traversal.

Here I've been lazy and asked the vscode plugin fitten code to write one for me.

<?php
function generateCombinations($attributes) {
    if (count($attributes) === 0) {
        return [[]];
    }
    $firstAttribute = array_shift($attributes);
    $combinationsWithoutFirst = generateCombinations($attributes);
    $combinations = [];
    foreach ($firstAttribute as $value) {
        foreach ($combinationsWithoutFirst as $combination) {
            $combinations[] = array_merge([$value], $combination);
        }
    }
    return $combinations;
}

Call it up and see.

<?php
$spec_list = [
    'memory' => ['16G', '32G', '64G'],
    'storage' => ['500G', '1T'],
    'graphics' => ['Integration', 'Independent'],
];
$combinations = generateCombinations($spec_list);
var_dump($combinations);

In terms of implementation results, it's fine.

It is true that AI changes the future ah, this generation speed than I manually knock much faster. But we can also find that it uses recursion, by definition, this ordinary specification attributes will not be too much, dozens of permutations and combinations has been a lot, and then more, the client side of the display will not look good, and the customer operation is not convenient, that recursive performance is not too bad.

But we are the pursuit of the ultimate ah, I'll write another loop to achieve, to avoid the program is too inefficient, see how to achieve it?

First you have to get a big loop and keep looping in it, and then you also have to determine if this combination has ever been made, and how does the number of layers work out?

I think you can first implement two layers, and then multi-layer, gradually increase the list of combinations, without further ado, see the code

<?php
function get_combine_list($spec_list) {
    if (count($spec_list) <= 1) {
        return $spec_list;
    }
    $result = [];
    $first_item = array_shift($spec_list);
    foreach ($first_item as $first_value) {
        $result[] = [$first_value];
    }
    while ($spec_list) {
        $tmp_result = [];
        $second = array_shift($spec_list);
        foreach ($result as $result_item) {
            foreach ($second as $second_value) {
                $tmp_result[] = array_merge($result_item, [$second_value]);
            }
        }
        $result = $tmp_result;
    }
    return $result;
}

Well, the same code calls it

<?php
$spec_list = [
    'memory' => ['16G', '32G', '64G'],
    'storage' => ['500G', '1T'],
    'graphics' => ['Integration', 'Independent'],
];
$combinations = get_combine_list($spec_list);

Found the results to be the same, big time!

The idea of the recursion above is to recursively find out if you're the last one, and then recursively again if you're not, eventually finding the last one, forming a combination, and then combining it with the penultimate one, and then combining it with the penultimate one.

My thinking here is to take out the first natural formation result, then take out the second to combine, then take out the third to combine with the existing results, until I take out the last specification to combine the final formation result assignment.

There are no advantages or disadvantages to the methods, only whether they are suitable and whether you can accept and maintain them yourself.