亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Reworded title: List all possible product options
P粉976737101
P粉976737101 2023-08-20 10:31:11
0
1
660
<p>以前,我可以輕松處理這樣的事情,但自從7年前離開開發(fā)世界以來,我失去了開發(fā)思維...</p> <p>我的情況是,我正在嘗試輸出客戶可以選擇的每個可能的產(chǎn)品選項以及生成的SKU - 由每個選項的SKU附加到產(chǎn)品的SKU。</p> <p>數(shù)據(jù)存儲得不好,因為這是一個相當古老的網(wǎng)站。</p> <p>下面是MySQL中存儲數(shù)據(jù)的示例,以及我在PHP中嘗試實現(xiàn)的目標。我將其限制為一個具有多個選項的產(chǎn)品。</p> <table class="s-table"> <thead> <tr> <th>products.id</th> <th>products.sku</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>a</td> </tr> </tbody> </table> <table class="s-table"> <thead> <tr> <th>options.id</th> <th>options.product_id</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>1</td> </tr> <tr> <td>2</td> <td>1</td> </tr> <tr> <td>3</td> <td>1</td> </tr> <tr> <td>4</td> <td>1</td> </tr> </tbody> </table> <table class="s-table"> <thead> <tr> <th>option_values.id</th> <th>option_values.option_id</th> <th>option_values.value</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>1</td> <td>b</td> </tr> <tr> <td>2</td> <td>1</td> <td>c</td> </tr> <tr> <td>3</td> <td>1</td> <td>d</td> </tr> <tr> <td>4</td> <td>2</td> <td>e</td> </tr> <tr> <td>5</td> <td>2</td> <td>f</td> </tr> <tr> <td>6</td> <td>3</td> <td>g</td> </tr> <tr> <td>7</td> <td>3</td> <td>h</td> </tr> <tr> <td>8</td> <td>4</td> <td>i</td> </tr> <tr> <td>9</td> <td>4</td> <td>j</td> </tr> <tr> <td>10</td> <td>4</td> <td>k</td> </tr> </tbody> </table> <p>迭代每個選項的每個可能組合,并輸出生成的SKU;</p> <table class="s-table"> <thead> <tr> <th>可能的SKUs</th> </tr> </thead> <tbody> <tr> <td>abegi</td> </tr> <tr> <td>acegi</td> </tr> <tr> <td>adegi</td> </tr> <tr> <td>abfgi</td> </tr> <tr> <td>acfgi</td> </tr> <tr> <td>adfgi</td> </tr> <tr> <td>abehi</td> </tr> <tr> <td>acehi</td> </tr> <tr> <td>adehi</td> </tr> <tr> <td>abegj</td> </tr> <tr> <td>acegj</td> </tr> <tr> <td>adegj</td> </tr> <tr> <td>abegk</td> </tr> <tr> <td>acegk</td> </tr> <tr> <td>adegk</td> </tr> <tr> <td>[等等]</td> </tr> </tbody> </table> <p>當我像這樣寫出來時,它似乎非常簡單,這讓我想知道我是否遺漏了什么...</p> <p>我目前正在迭代每個產(chǎn)品,對于每個產(chǎn)品的每個選項,然后對于每個選項的每個值,但顯然這不能滿足每種可能的情況。</p> <p>DB Fiddle - https://www.db-fiddle.com/f/vHWiKsKi9WUvvDwAa6pqw6/0</p> <p>謝謝!</p>
P粉976737101
P粉976737101

reply all(1)
P粉046878197

The function that creates the Cartesian product of all options is inspired by the answer to this question.

<?php

function all_skus(array $product) {
    $skus = [];
    $result = [[]];

    foreach ($product['options'] as $key => $option) {
        $append = [];
        foreach ($result as $options) {
            foreach ($option as $value) {
                $append[] = $options + [$key => $value];
            }
        }
        $result = $append;
    }

    foreach ($result as $option_set) {
        $skus[] = $product['sku'] . implode($option_set);
    }

    return $skus;
}


$pdo = new PDO(/* your stuff here */);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

$res = $pdo->query('
            SELECT `o`.`product_id`, `p`.`sku`, `ov`.`option_id`, `ov`.`value`
            FROM `products` `p`
            JOIN `options` `o` ON `p`.`id` = `o`.`product_id`
            JOIN `option_values` `ov` ON `o`.`id` = `ov`.`option_id`'
        );

$nested = [];
foreach ($res as $row) {
    $nested[$row->product_id]['options'][$row->option_id][] = $row->value;
    $nested[$row->product_id]['sku'] = $row->sku;
}

$skus = [];
foreach ($nested as $i => $product) {
    $skus = array_merge($skus, all_skus($product));
    unset($nested[$i]);
}

var_dump($skus);

If you are only interested in the sku string, you can simplify the function to:

function all_skus(array $product) {
    $result = [$product['sku']];

    foreach ($product['options'] as $option) {
        $append = [];
        foreach ($result as $options) {
            foreach ($option as $value) {
                $append[] = $options . $value;
            }
        }
        $result = $append;
    }

    return $result;
}

I'm sure someone can provide a more efficient answer, but this answer produces the desired output based on your example data.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template