根据选项组和选项计算产品变体
我正在编写一个电子商务网站,需要一种好方法来计算产品变化。网站有产品,产品可以有很多选项组,选项组可以有很多选项。
因此,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以解决问题:
测试:http://www.ideone.com/NZE5S
This should do the trick:
Tested: http://www.ideone.com/NZE5S
如果这是一个电子商务网站,我猜测您的选项组已经在 SQL 数据库中,那么为什么不让 SQL 为您进行组合呢。
但是,如果您将所有选项都放在一个表中,并具有其所在组的外键,该怎么办...
一旦您拥有一个包含组 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.
But what if you had all your options in one table with a foreign key to the group it's in...
Once you have an array containing the group IDs generating the SQL statement above is trivial (just concatenating a few string implodes).