Php 浏览一组数据的最佳方法
我正在查看一些成本信息,我需要一种显示所有组合的好方法。我不确定是否必须使用嵌套的Php 浏览一组数据的最佳方法,php,arrays,foreach,Php,Arrays,Foreach,我正在查看一些成本信息,我需要一种显示所有组合的好方法。我不确定是否必须使用嵌套的foreach循环或其他方法 我有25种不同的东西项目1-项目2第25项 我有6个螺栓,用于我的项目建设螺栓1-螺栓2-螺栓3-螺栓4-螺栓5-螺栓6每个螺栓的成本不同 所有项目只能使用一种螺栓。不是一种混合物,如螺栓1的3和螺栓2的3 25个项目中的7个需要6个螺栓 其余6个需要5个螺栓 其余3个需要4个螺栓 其余5个需要3个螺栓 其余3个需要2个螺栓 最后1个需要1个螺栓 这就是它变得棘手的地方 每个项目及
foreach
循环或其他方法
我有25种不同的东西项目1-项目2第25项
我有6个螺栓,用于我的项目建设螺栓1-螺栓2-螺栓3-螺栓4-螺栓5-螺栓6每个螺栓的成本不同
所有项目只能使用一种螺栓。不是一种混合物,如螺栓1的3和螺栓2的3
- 25个项目中的7个需要6个螺栓
- 其余6个需要5个螺栓
- 其余3个需要4个螺栓
- 其余5个需要3个螺栓
- 其余3个需要2个螺栓
- 最后1个需要1个螺栓
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有这样的内置函数吗?请提供您能提供的任何见解,我们将不胜感激。还有别的方法吗?我认为你的问题更多的是内容建模,而不是实际的编程 您是否考虑过使用mysql数据库来存储这些关系?你有一张桌子放物品,一张桌子放螺栓,一张桌子放附件,一张桌子放填充器,一张桌子放包装器 然后,您应该定义每个对象之间的关系:
- 1对1:示例:“一个人就像一对眼睛。一对眼睛只能属于一个人”
- 一对多:例如:“一个人只能有一个家庭”
- 多对一:例如:“一个家庭可以有很多人。”
- 多对多:例如:“一个食谱可以有许多配料。一个配料可以有许多食谱”
我意识到我是在回应你的要求,把你带到一个完全不同的方向,但我会这样做。我会使用MVC框架来加速开发(cakephp、yii、codeigniter等)。如果您需要有关此方法的更多信息,请告诉我。我认为您的问题更多的是内容建模,而不是实际的编程 您是否考虑过使用mysql数据库来存储这些关系?你有一张桌子放物品,一张桌子放螺栓,一张桌子放附件,一张桌子放填充器,一张桌子放包装器 然后,您应该定义每个对象之间的关系:
- 1对1:示例:“一个人就像一对眼睛。一对眼睛只能属于一个人”
- 一对多:例如:“一个人只能有一个家庭”
- 多对一:例如:“一个家庭可以有很多人。”
- 多对多:例如:“一个食谱可以有许多配料。一个配料可以有许多食谱”
我意识到我是在回应你的要求,把你带到一个完全不同的方向,但我会这样做。我会使用MVC框架来加速开发(cakephp、yii、codeigniter等)。如果您需要有关此方法的更多信息,请与我联系。让我看看如何按照您的建议使用数组。我们将使用多维数组,以便您可以存储关于每个零件的更多信息,而不仅仅是价格(如名称、提供商、url等)
这将是编写它的最简单但可维护的方法。让我们看看如何使用数组,正如您所建议的那样。我们将使用多维数组,以便您可以存储关于每个零件的更多信息,而不仅仅是价格(如名称、提供商、url等)
这大概是最简单但可维护的编写方法。正如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);
我们的工作:
// 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);