我正在撰写电子商务网站,需要一种很好的方法来计算产品差异.该网站有产品,产品可以有很多选项组,选项组可以有很多选项.
所以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)
{ …Run Code Online (Sandbox Code Playgroud)