1
你好,我有一個邏輯的問題,使我的測試完成 這是我的數組拆分PHP和循環邏輯
Array (
[0] => Array (
[id] => 1,
[p] => 150,
[w] => 120,
),
[1] => Array (
[id] => 2,
[p] => 10,
[w] => 20,
),
[2] => Array (
[id] => 3,
[p] => 70,
[w] => 10,
),
[3] => Array (
[id] => 4,
[p] => 100,
[w] => 45,
),
[4] => Array (
[id] => 5,
[p] => 110,
[w] => 500,
)
)
更新代碼
$pt = 0;
foreach($data as $k => $plist){
$pl[] = $plist['id'];
$pt += $plist['p'];
if($pt >= 250) break;
}
// Get filter max p 250
foreach($data $k => $dat){
foreach($pl as $paa => $pat){
unset($data[$paa]); // Delete list key of Data
}
$jres[] = array("id" => $dat['id'],"p" => $dat['p'], "w" => $dat['w']);
}
// callback function and filter that will be printed only
foreach($jres as $k => $dat){
foreach($data as $paa => $pat){
unset($jres[$paa]); // Delete list key of JRES
}
}
foreach($jres as $k => $dat){
$subtotal += $dat['p'];
}
$subs = $subtotal + $pt;
end($pl);
$end = key($pl);
if($subs > 250){
unset($pl[$end]); // delete one key if total **p** sum total **price * w ** if above 250 will delete one key
}
$pack = '<table class="table">
<thead>
<h3 style="background:#D8D8D8;">Package 1</h3>
<tr>
<td class="right">P</td>
<td class="right">W</td>
</tr>
</thead><tbody>';
foreach($jres as $k => $dat){
$subtotal += $dat['p'];
$subw += $dat['w'];
$list .='<tr>
<td class="right">$'.$dat['p'].'</td>
<td class="right">'.$dat['w'].$k.' gram</td>
</tr>';
foreach($data as $dat2 => $pat){
if($k == $dat2) unset($data[$k]); // Delete key from data
}
}
$gtotal = $subw * $sfee + $subtotal;
$total = '<tr>
<th class="right" colspan="2"> Subtotal: </th>
<th class="right"> $'.$subtotal.' </th>
</tr>
<tr>
<th class="right" colspan="2">Shipping Cost ['.$subw.'g]: </th>
<th class="right"> $'.$subw * $sfee.' </th>
</tr>
<tr>
<th class="right" colspan="2"> Grandtotal: </th>
<th class="right"> $'.$gtotal.' </th>
</tr>
<tr>
<th class="right"></th>
<th class="right"></th>
</tr>
</tbody>
</table>';
的目標是如何找到最小的「W 「在視圖分割中
如果總計爲250,則每個分割視圖的」p「的最大值
價格/W是0.25將總和與總P的
所以我要嘗試用循環一些邏輯,但它如此複雜,無法正常工作
你能不能幫我寫的邏輯這個的?
我上面的代碼只是顯示第一個分割視圖。
樣本輸出我希望它是這樣的:
First Split
| id| w | p |
| 2| 20| 10|
| 3| 10| 70|
| 4| 45|100|
| stotal|180|
| wtotal|18.75| // $w = 20 + 10 + 45 * 0.25;
| gtotal|198.75|
SecondSplit
| id| w | p |
| 1|120|150|
| stotal|150|
| wtotal| 30| // $w = 120 * 0.25;
|Gtotal |180|
Third Split
| id| w | p |
| 5|500|110|
| stotal|110|
| wtotal|125| // $w = 500 * 0.25;
|Gtotal |235|
嗨,也許你可以告訴我們你嘗試過什麼使用循環,所以我們可以從那裏開始,看看別人能否爲您提供更好的解決方案。 – kdlcruz
如果你現在沒有發佈你正在使用的代碼,你至少應該發佈一個可用的PHP數組,供人們用來爲你粉碎一些東西 - print_r的輸出沒有多大用處,手動重寫數組 –