【问题标题】:Asociate existing product variants with new generated variants将现有产品变体与新生成的变体相关联
【发布时间】:2019-03-19 16:49:49
【问题描述】:

我想为自己的电子商务网站创建产品变体。

当我想知道哪个变体是现有的,哪个是新的,插入或更新它时,我有一个问题。

要创建变体,我使用以下 php 函数:

<pre>
function possible_combos($groups, $prefix='') {
    $result = array();
    $group = array_shift($groups);

    foreach($group as $selected) {
        if($groups) {
            $result = array_merge($result, possible_combos2($groups, $prefix .$selected.', '));
        } else {
            $result[] = [
                'combination_string' => $prefix.$selected,
            ];
        }
    }
    return $result;
}
</pre>

使用以下数组进行构建:

<pre>
Array
(
    [Color] => Array
        (
            [0] => Green
            [1] => Red
        )

    [Storage] => Array
        (
            [0] => 128 GB
            [1] => 256 GB
        )

)
</pre>

颜色:绿色、红色 存储:128 GB、256 GB

结果是:

<pre>
Array
(
    [0] => Array
        (
            [combination_string] => Green, 128 GB
        )

    [1] => Array
        (
            [combination_string] => Green, 256 GB
        )

    [2] => Array
        (
            [combination_string] => Red, 128 GB
        )

    [3] => Array
        (
            [combination_string] => Red, 256 GB
        )

)
</pre>

在添加模式下一切正常,问题在于编辑模式。

对于每个变体组合,我使用“Parent_id”创建一个新产品;

所以在编辑页面有这个:

<pre>
Array
(
    [0] => Array
        (
            [product_id] => 1,
            [combination_string] => Green, 128 GB
        )

    [1] => Array
        (
        [product_id] => 2,
            [combination_string] => Green, 256 GB
        )

    [2] => Array
        (
            [product_id] => 3,
            [combination_string] => Red, 128 GB
        )

    [3] => Array
        (
            [product_id] => 4,
            [combination_string] => Red, 256 GB
        )

)
</pre>

如果我为组合添加新属性,例如:

尺寸:大

<pre>
Array
(
    [Color] => Array
        (
            [0] => Green
            [1] => Red
        )

    [Storage] => Array
        (
            [0] => 128 GB
            [1] => 256 GB
        )
    [Size] => Array
        (
            [0] => Big
        )

)
</pre>

对于这种情况我想返回

<pre>
Array
(
    [0] => Array
        (
            [product_id] => 1,
            [combination_string] => Green, 128 GB, Big
        )

    [1] => Array
        (
            [product_id] => 2,
            [combination_string] => Green, 256 GB, Big
        )

    [2] => Array
        (
            [product_id] => 3,
            [combination_string] => Red, 128 GB, Big
        )

    [3] => Array
        (
            [product_id] => 4,
            [combination_string] => Red, 256 GB, Big
        )
)
</pre>

而且...如果我为属性 Storage 512 GB 添加一个新值,例如: 结果应如下所示:

<pre>
Array
(
    [0] => Array
        (
            [product_id] => 1,
            [combination_string] => Green, 128 GB, Big
        )

    [1] => Array
        (
            [product_id] => 2,
            [combination_string] => Green, 256 GB, Big
        )

    [2] => Array
        (
            [combination_string] => Green, 512 GB, Big
        )

    [3] => Array
        (
            [product_id] => 3,
            [combination_string] => Red, 128 GB, Big
        )

    [4] => Array
        (
            [product_id] => 4,
            [combination_string] => Red, 256 GB, Big
        )

    [5] => Array
        (
            [combination_string] => Red, 512 GB, Big
        )

)
</pre>

现有梳子没有 product_id。

我不知道我是否清楚这一点,但如果需要,我可以提供更多信息。

编辑 @PHPnoob

【问题讨论】:

    标签: php arrays combinations


    【解决方案1】:

    您需要一个可以进行多个数组排列的函数。

    我将简单地粘贴在相应的函数下方from an another SO thread

    function cartesian($input) {
        $result = array(array());
    
        foreach ($input as $key => $values) {
            $append = array();
    
            foreach($result as $product) {
                foreach($values as $item) {
                    $product[$key] = $item;
                    $append[] = $product;
                }
            }
    
            $result = $append;
        }
    
        return $result;
    }
    

    【讨论】:

    • 谢谢你的回答,但我有笛卡尔函数,我的问题是我怎么知道哪个组合是新的,哪个是旧的。我需要一种将 DB 组合查询数组与新组合数组合并的方法。
    • 你打算如何从这个表中过滤,因为唯一的区别是在单列“name”中进行的?猜猜你必须遍历每一行并插入新的如果它还不存在......
    • 现在对我来说看起来更奇怪了。案例 2:为什么有一个父 id 为 0 和三个设置为 1 ?案例 3:你不能指望两个 id 相等,因为根据定义它们是唯一的......
    • 因此,您要么正确地重新构建数据库,要么尝试使用它,因此您需要将“product_name”列设置为唯一并使用 MySQL INSERT IGNORE 语句。
    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    相关资源
    最近更新 更多