2

我正在查看一些成本信息,我需要一种显示所有组合的好方法。我不确定我是否必须使用嵌套foreach循环或其他东西。

我有 25 种不同的物品。项目 1 -项目 2 ..项目 25

我有 6 个螺栓用于构建我的物品。bolt1 - bolt2 - bolt3 - bolt4 - bolt5 - bolt6每个螺栓都有不同的成本。

所有物品只能使用一种螺栓。不是混合物,例如螺栓 1 的 3 和螺栓 2 的 3。

  • 25 个项目中有 7 个需要 6 个螺栓。
  • 其余 6 个需要 5 个螺栓。
  • 其余 3 个需要 4 个螺栓。
  • 其余 5 个需要 3 个螺栓。
  • 其余 3 个需要 2 个螺栓。
  • 最后 1 个需要 1 个螺栓。

这就是棘手的地方。

每件带有螺栓数量的物品都需要 4 个配件包中的一个,这些配件包的价格不同。

acc1 - acc2 - acc3 - acc4

每个项目还需要 4 个盒子填充器之一。每个,都有不同的成本

填充器 1 -填充器 3 -填充器 3 -填充器 4

最后,每个项目都需要 3 个包装器之一。每个,都有不同的成本。

包装器1 -包装器2 -包装器3

例如,我需要 PHP 给我所有可能的价格组合。

foreach 6 bolt item{
(6 bolt1 * $cost) +  ($acc1Cost + $stuffer1Cost + $wrapper1Cost) = item1acc1stff1wra1COST
(6 bolt1 * $cost) +  ($acc2Cost + $stuffer1Cost + $wrapper1Cost) = item1acc2stff1wra1COST
(6 bolt1 * $cost) +  ($acc3Cost + $stuffer1Cost + $wrapper1Cost)  ..
(6 bolt1 * $cost) +  ($acc4Cost + $stuffer1Cost + $wrapper1Cost)  ..
(6 bolt1 * $cost) +  ($acc1Cost + $stuffer2Cost + $wrapper1Cost)  ..
(6 bolt1 * $cost) +  ($acc1Cost + $stuffer3Cost + $wrapper1Cost)
(6 bolt1 * $cost) +  ($acc1Cost + $stuffer4Cost + $wrapper1Cost)
(6 bolt1 * $cost) +  ($acc1Cost + $stuffer1Cost + $wrapper2Cost)
(6 bolt1 * $cost) +  ($acc1Cost + $stuffer1Cost + $wrapper3Cost)
(6 bolt2 * $cost) +  ($acc1Cost + $stuffer1Cost + $wrapper3Cost)
(6 bolt2 * $cost) +  ($acc1Cost + $stuffer1Cost + $wrapper3Cost)
..... all combinations
foreach 5 bolt item{
(5 bolts * $cost) +  (**$acc1Cost** + **$stuffer1Cost** **$wrapper1Cost**)
(5 bolts * $cost) +  (**$acc2Cost** + **$stuffer1Cost** **$wrapper1Cost**)
(5 bolts * $cost) +  (**$acc3Cost** + **$stuffer1Cost** **$wrapper1Cost**)
(5 bolts * $cost) +  (**$acc4Cost** + **$stuffer1Cost** **$wrapper1Cost**)

..so on to the end..
}

每种类型的项目都被分类到不同的数组中,这些数组除以它们需要多少个螺栓。

$oneBolt = array(
bolt1 => cost,
...
);

其他每个项目组都保存在各自的价格数组中。

我意识到我可以像上面一样写出所有组合,但是如何使用循环来做到这一点?以一种或另一种方式做到这一点是否明智?PHP 有这样的内置函数吗?请,您可以提供的任何见解都将受到赞赏。我应该这样做吗?

4

3 回答 3

2

按照您的建议,让我们看看如何使用数组来做到这一点。我们将使用多维数组,以便您可以存储除价格之外的每个部分的更多信息(如名称、提供者、url 等)。

$bolts = array();
$bolts[1] = array('price'=> 25);
$bolts[2] = array('price'=> 12);
$bolts[3] = array('price'=> 45);
$bolts[4] = array('price'=> 33);
$bolts[5] = array('price'=> 32);
$bolts[6] = array('price'=> 31);

$accessories = array();
$accessories[1] = array('price'=> 140);
$accessories[2] = array('price'=> 120);
$accessories[3] = array('price'=> 134);
$accessories[4] = array('price'=> 136);

$stuffer = array();
$stuffer[1] =array('price'=> 12);
$stuffer[2] =array('price'=> 12);
$stuffer[3] =array('price'=> 12);
$stuffer[4] =array('price'=> 12);

$wrapper = array();
$wrapper[1] =array('price'=> 12);
$wrapper[2] =array('price'=> 12);
$wrapper[3] =array('price'=> 12);

$items = array();
$items[1] = array('bolt'=> 3, 'bolt_amount'=>7, 'accessory'=>2, 'stuffer'=>4, 'wrapper'=>2);
// Do that for 25 items, each item is an array containing its relationships. Store the Index of the part in its own array so you can reference it when calculating the price.

$items[1]['price'] = ($items[1]['bolt_amount'] * $bolts[$items[1]['bolt']]['price']) + $accessories[$items[1]['accessory']]['price'] + $stuffer[$items[1]['stuffer']]['price'] + $wrapper[$items[1]['wrapper']]['price'];

echo $items[1]['price'];

// 7*45 + 120 + 12 + 12 = 459

那将是最简单但可维护的编写方式。

于 2012-08-11T23:44:10.487 回答
2

我认为您的问题更多是关于内容建模而不是实际编程。

您是否考虑过使用 mysql 数据库来存储这些关系?您将有一张用于物品的桌子,一张用于螺栓的桌子,一张用于附件的桌子,一张用于填充物的桌子和一张用于包装纸的桌子。

然后,您应该定义每个对象之间的关系:

  • 1对1:例如:“一个人作为一组眼睛。一组眼睛只能属于一个人”
  • 一对多:例如:“一个人只能有一个家庭”
  • 多对一:例如:“一个家庭可以有很多人。”
  • 多对多:例如:“一个食谱可以有很多成分。一个成分可以有很多食谱”

所以,在你的情况下:

物品/螺栓关系是:多对一物品/配件是:一对一(afaik)等。

设置完成后,您需要生成实现“业务逻辑”的中间件代码(例如 php):确保遵守您的限制,然后根据这些限制计算每个项目的价格。

我意识到我正在通过将您带入一个完全不同的方向来回应您的要求,但这就是我会这样做的方式。我会使用 MVC 框架来加速开发(cakephp、yii、codeigniter 等)。如果您需要有关此方法的更多信息,请告诉我。

于 2012-08-11T23:21:07.177 回答
0

正如pixeline所写,如果您使用数据库会更好。但是如果你想使用数组,我写了几行对你有用的代码。

// make combinations array
function combinations($data, &$all = array(), $group = array(), $val = null, $i = 0) {
    if (isset($val))
    {
        array_push($group, $val);
    }

    if ($i >= count($data))
    {
        $combination_price = array_product($group);
        array_push($all, array('combination' => $group, 'combination_price' => $combination_price));
    }
    else {
        foreach ($data[$i] as $v)
        {
            combinations($data, $all, $group, $v, $i + 1);
        }
    }

    return $all;
}

$bolts = array(
    25, // bolt 1 price
    30, // bolt 2 price
    40  // bolt 3 price
);

// array of items contains number of bolts and price
$items = array (
    array(
        'bolts_num' => 6,
        'bolt_price' => $bolts[0]
    ), 
    array(
        'bolts_num' => 6,
        'bolt_price' => $bolts[1]
    ), 
    array(
        'bolts_num' => 6,
        'bolt_price' => $bolts[2]
    ),  
);

$accessories = array(
    12, 16, 18, 20 //accessories prices
);

$stuffers = array(
    20, 10, 30, 40  //stuffers prices
);

$wrapper = array(
    11, 13, 17  //wrapper prices
);

// all combinations, use var_dump if you dont know what is inside
$combinations = combinations(array($accessories, $stuffers, $wrapper));

$item_combinations = array();

// join combinations and items and calculate final price
foreach ($items as $item_key => $item) {
    foreach($combinations as $combination) {
        $item_price = array_product($item);
        $item_combinations[] = array(
            'item_id' => $item_key,
            'combination' => $combination['combination'],
            'price' => $combination['combination_price'] + $item_price
        );
    }
}

var_dump($item_combinations);

我们所做的:

  1. 创建所有配件 - 填充物 - 包装组合并计算每个组合的价格(配件*填充物*包装)。我使用了 array_product 函数。
  2. 加入组合和项目。我计算了每件商品的价格(螺栓 * 数量)并添加到组合价格中。我忘记了商品价格,所以您也应该添加商品价格。

如果您有任何问题 - 问。

查看实际操作:http ://codepad.viper-7.com/8fsVp6

于 2012-08-12T00:22:38.207 回答