2012-08-11 18 views
2

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

我有25个不同的项目。 物品1 - ITEM2 .. 项目25

我有一个在我的项目建设使用了6个螺栓。 bolt1 - bolt2 - bolt3 - bolt4 - bolt5 - bolt6每个螺栓具有不同的成本。

所有项目只能使用一个类型的螺栓。不是混合物,像3号螺栓1和3号螺栓。

  • 25件物品中的7件需要6个螺栓。
  • 其余6个需要5个螺栓。
  • 其余3个需要4个螺栓。
  • 其余5个需要3个螺栓。
  • 其余3个需要2个螺栓。
  • 最后1个需要1个螺栓。

这里是棘手的地方。

每个项目与他们的螺栓数量需要4个附件包中的一个具有不同的成本。

ACC1 - ACC2 - ACC3 - ACC4

每个项目也需要4盒填充之一。每次,具有不同的成本

stuffer1 - stuffer3 - stuffer3 - stuffer4

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

wrapper1 - wrapper2 - wrapper3

我需要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是否有这样的内置函数?请,任何洞察力,你可以提供表示赞赏。还有另外一种方法我应该这样做吗?

回答

2

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

你有没有考虑过使用mysql数据库来存储这些关系?你会有一张桌子上的物品,一张桌子上的螺栓,一张桌子上的配件,一张桌子,一张桌子,还有一张桌子。

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

  • 1到1:例如:“一个人的一组眼睛的眼一组只能属于一个人的”
  • 一对多:例如:“一个人只能有一个家庭”
  • 多对一:例如:“一个家庭可以有很多人。”
  • 多到很多:例如:“配方可以有许多配料的成分,可以有很多的食谱。”

所以,你的情况:

笔数/螺栓的关系是:多对一一对一(afaik) 等

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

我意识到我正在回应你的要求,让你进入一个完全不同的方向,但这就是我会这么做的。我会使用MVC框架来加速开发(cakephp,yii,codeigniter等)。让我知道你是否需要更多关于这种方法的信息。

+0

我可能需要指出的是,价格被拉到另一个网站的卷曲刮。目标是找到价格最低的组合以触发另一个步骤,那就是需求的程度。我不知道这是否会改变你的答案,但我认为这是相关的。我正在研究MVC的东西,但我不确定。 – KiloJKilo 2012-08-11 23:29:50

+0

我已经发布了一个替代答案,只使用数组。看看它是否符合你的想法。 – pixeline 2012-08-11 23:44:40

2

让我们看看如何使用数组来实现,就像你所建议的那样。我们将使用多维数组,以便您可以存储除价格以外的每个部分的更多信息(如名称,提供者,网址等)。

$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 

这将是最简单的 - 但可维护 - 的方式来写它。

+0

我会研究答案和试验。我会在几天内接触基地。谢谢 – KiloJKilo 2012-08-11 23:59:22

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

相关问题