将现有产品变体与新生成的变体相关联

时间:2019-03-19 16:49:49

标签: php arrays combinations

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

当我想知道哪个变体存在而哪个变体要插入或更新时,我有一个探测。

要创建变体,请使用以下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>

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

对于每个变体组合,我都使用“ 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>

然后...如果我为示例属性 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

1 个答案:

答案 0 :(得分:0)

您需要一个可以进行多个数组置换的函数。

我将简单地粘贴到相应的功能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;
}