我有3列表,客户1,客户2和客户3,上限值如200,300和500总计1000.我有一个数组中的8个不同的值,如150,150,150,150,100,100,100,50,50,我必须分发这些价值观对每个客户而言都和posible一样好(不要交叉限制)。
我试图在拆分之前制作表格
Before Split
<?php
$customer_count = 3;
$customer_limit = array('200','300','500');
$values_array = array('150','150','150','150','100','100','100','50','50')
?>
<table width="50%" border="1">
<tr>
<?php
for($i=1;$i<=$customer_count;$i++){
echo "<td>Customer ".$i."</td>";
}
?>
</tr>
<tr>
<?php
for($i=0;$i<$customer_count;$i++){
echo "<td>".$customer_limit[$i]."</td>";
}
?>
</tr>
<tr>
<td>
<table width="100%" border="1">
<?php
foreach($values_array as $val){
?>
<tr>
<td><?=$val;?></td>
</tr>
<?php
}
?>
</table>
</td>
<td>
<table width="100%" border="1">
<?php
foreach($values_array as $val){
?>
<tr>
<td><?=$val;?></td>
</tr>
<?php
}
?>
</table>
</td>
<td>
<table width="100%" border="1">
<?php
foreach($values_array as $val){
?>
<tr>
<td><?=$val;?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
所以我给了我一个关于如何制作&#34;分裂后#34;表格如图所示。
答案 0 :(得分:1)
你不需要在这里使用这么多循环。我的方法有点不同,但它肯定能解决目的。
我们需要一个嵌套循环,一个用于客户,一个用于值。
首先,我们可以循环并将数据存储在名为$ customer的变量中,该变量存储所有3个客户的数据及其各自的分布值(请记住其最大限制)。
for ($i = 0; $i < count($customer_limit); $i++) {
foreach ($values_array as $k => $value) {
$pre_existing = isset($customer[$i]) ? array_sum($customer[$i]) : 0; // If customer has any previous value, calculate the sum, or else make it 0(for the first time).
if (($pre_existing+$value) <= $customer_limit[$i]) { // Existing value + the current value must be less than the specified customer limit
$customer[$i][] = @$value; // Values are stored
unset($values_array[$k]); // Currently used value is deleted from the original array, considering the value used by one customer can't be used by any other customer.
}
}
}
echo "<pre>";
print_r($customer);
三位客户的详情:
Array
(
[0] => Array
(
[0] => 150
[1] => 50
)
[1] => Array
(
[0] => 150
[1] => 150
)
[2] => Array
(
[0] => 150
[1] => 100
[2] => 100
[3] => 100
[4] => 50
)
)