根据选项组和选项计算产品变体

发布于 2024-12-03 00:10:54 字数 2109 浏览 1 评论 0原文

我正在编写一个电子商务网站,需要一种好方法来计算产品变化。网站有产品,产品可以有很多选项组,选项组可以有很多选项。

因此,T 恤产品有 3 个选项组和选项:

尺寸: 小的, 中等的, 大号,

颜色: 红色的, 蓝色的, 黄色的, 黑色,

材质: 棉布, 尼龙,

它创建:小红色棉布,小红色尼龙,小蓝色棉布,小蓝色尼龙,......等等

我知道下面的脚本有效,而且它可以优化。谁能提供一个更好的工作示例吗?使用递归也应该是可能的......但我遇到了心理障碍。

    if(count($option_groups) > 1)
    {
        // start the variants up
        foreach($option_groups[0]->get_options() as $option)
        {
            $variants[] = array($option);
        }

        // go through every other option group to make combos
        for($x = 1; $x < count($option_groups); $x++)
        {
            $combos = array();

            foreach($variants as $variant)
            {
                $new = array();
                foreach($option_groups[$x]->get_options() as $option)
                {
                    $tmp        = $variant;
                    $tmp[]  = $option;
                    $new[]  = $tmp;
                }
                $combos[] = $new;
            }
            $variants = array();
            foreach($combos as $combo)
            {
                foreach($combo as $tmp)
                {
                    $variants[] = $tmp;
                }
            }
        }
    }

这不是超级时间敏感的,但我想要有更多可维护的代码块,这非常恶心。

这个问题(我觉得这不是一个原始问题,很多车都这样做)有名字吗?我没有在谷歌上找到任何关于这个问题的信息。

编辑 这就是我最终得到的结果,它基于profitphp的解决方案,但维护了我的对象,而不是为我提供连接为字符串的每个变体的选项。这一切都归功于Profitphp!

private function _possible_combos($groups, $prefix = array())
{
    $result = array();
    $group  = array_shift($groups);
    foreach($group->get_options() as $selected)
    {
        if($groups)
        {
            $tmp            = $prefix;
            $tmp[]      = $selected;
          $result = array_merge($result, $this->_possible_combos($groups, $tmp));
        }
        else
        {
            $tmp            = $prefix;
            $tmp[]      = $selected;
          $result[] = $tmp; 
        }
    }

    return $result;
}

I am writing an ecommerce site, and need a good way to calculate product variations. The Site has products, products can have many option groups, option groups can have many options.

So a Tshirt Product has 3 option groups and options:

Size:
Small,
Medium,
Large,

Color:
Red,
Blue,
Yellow,
Black,

Material:
Cotton,
Nylon,

which creates: small red cotton, small red nylon, small blue cotton, small blue nylon, ... so on and so forth

I know that the script below works, but also that it can be optimized. Can anyone furnish a better working example of this? It should be possible using recursion as well... but I'm hitting a mental block.

    if(count($option_groups) > 1)
    {
        // start the variants up
        foreach($option_groups[0]->get_options() as $option)
        {
            $variants[] = array($option);
        }

        // go through every other option group to make combos
        for($x = 1; $x < count($option_groups); $x++)
        {
            $combos = array();

            foreach($variants as $variant)
            {
                $new = array();
                foreach($option_groups[$x]->get_options() as $option)
                {
                    $tmp        = $variant;
                    $tmp[]  = $option;
                    $new[]  = $tmp;
                }
                $combos[] = $new;
            }
            $variants = array();
            foreach($combos as $combo)
            {
                foreach($combo as $tmp)
                {
                    $variants[] = $tmp;
                }
            }
        }
    }

This isn't super time sensitive, but I'd like to have a more maintainable chunk of code, this is pretty gross.

Also does this problem (I feel like it isn't an original problem, many carts do this) have a name? I wasn't pulling anything up on google for this problem.

EDIT
This is what I ended up with, its based off of profitphp's solution, but maintains my objects instead of giving me the options per variant concatenated as a string. All thanks to Profitphp!

private function _possible_combos($groups, $prefix = array())
{
    $result = array();
    $group  = array_shift($groups);
    foreach($group->get_options() as $selected)
    {
        if($groups)
        {
            $tmp            = $prefix;
            $tmp[]      = $selected;
          $result = array_merge($result, $this->_possible_combos($groups, $tmp));
        }
        else
        {
            $tmp            = $prefix;
            $tmp[]      = $selected;
          $result[] = $tmp; 
        }
    }

    return $result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚雾 2024-12-10 00:10:54

这应该可以解决问题:

<?

$data[]=array('shirt');
$data[]=array('red','yellow','black');
$data[]=array('small','medium','large');

$combos=possible_combos($data);

//calculate all the possible comobos creatable from a given choices array
function possible_combos($groups, $prefix='') {
    $result = array();
    $group = array_shift($groups);
    foreach($group as $selected) {
        if($groups) {
            $result = array_merge($result, possible_combos($groups, $prefix . $selected. ' '));
        } else {
            $result[] = $prefix . $selected;
        }
    }
    return $result;
}

echo count($combos) . "\n";
print_r($combos);

测试:http://www.ideone.com/NZE5S

This should do the trick:

<?

$data[]=array('shirt');
$data[]=array('red','yellow','black');
$data[]=array('small','medium','large');

$combos=possible_combos($data);

//calculate all the possible comobos creatable from a given choices array
function possible_combos($groups, $prefix='') {
    $result = array();
    $group = array_shift($groups);
    foreach($group as $selected) {
        if($groups) {
            $result = array_merge($result, possible_combos($groups, $prefix . $selected. ' '));
        } else {
            $result[] = $prefix . $selected;
        }
    }
    return $result;
}

echo count($combos) . "\n";
print_r($combos);

Tested: http://www.ideone.com/NZE5S

最美的太阳 2024-12-10 00:10:54

如果这是一个电子商务网站,我猜测您的选项组已经在 SQL 数据库中,那么为什么不让 SQL 为您进行组合呢。

SELECT Size.Name, Color.Name, Material.Name FROM Size, Color, Material

但是,如果您将所有选项都放在一个表中,并具有其所在组的外键,该怎么办...

SELECT r1.Name, r2.Name, r3.Name 
FROM Options r1, Options r2, Options r3
WHERE r1.GroupID = 1 -- id for Size
    AND r2.GroupID = 2 -- id for Color
    AND r3.GroupID = 3 -- id for Material

一旦您拥有一个包含组 ID 的数组,生成上面的 SQL 语句就很简单了(只需连接几个字符串内爆)。

If this is an e-commerce site my guess is your option groups are already in an SQL database so why not just let SQL do the combinations for you.

SELECT Size.Name, Color.Name, Material.Name FROM Size, Color, Material

But what if you had all your options in one table with a foreign key to the group it's in...

SELECT r1.Name, r2.Name, r3.Name 
FROM Options r1, Options r2, Options r3
WHERE r1.GroupID = 1 -- id for Size
    AND r2.GroupID = 2 -- id for Color
    AND r3.GroupID = 3 -- id for Material

Once you have an array containing the group IDs generating the SQL statement above is trivial (just concatenating a few string implodes).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文