2012-08-11 19 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

相關問題