【问题标题】:calculate product variants based on option groups and options根据选项组和选项计算产品变体
【发布时间】:2011-09-06 17:57:26
【问题描述】:

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

所以一个 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;
}

【问题讨论】:

  • 很有趣,这是我在面试时提出的问题,足够接近了。
  • 不要以为你得到了任何好的(或至少比我写的更好)答案?
  • 作为一个 Haskell 布道者,我不禁吹嘘这个功能是内置的:sequence [["small", "medium", "large"], ["red", "blue", "yellow", "black"], ["cotton", "nylon"]] = [["small","red","cotton"],["small","red","nylon"],["small","blue","cotton"],["small","blue","nylon"], {- and a bunch more ... -}]
  • 如果 php 有这个功能那就太好了。

标签: php algorithm e-commerce


【解决方案1】:

这应该可以解决问题:

<?

$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

【讨论】:

    【解决方案2】:

    如果这是一个电子商务网站,我猜你的选项组已经在 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 语句就很简单了(只是连接几个字符串内爆)。

    【讨论】:

    • 您可能正在做某事,但我认为您误会了我的架构:Product(id_product,...), OptionGroup(id_option_group, id_product, name), Option(id_option, id_option_group, name) - 产品的选项组没有为好主意设置 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 2020-09-05
    相关资源
    最近更新 更多