繁体   English   中英

用循环逻辑拆分PHP

[英]Split php with loop logic

您好,我有一个逻辑问题要完成测试,这是我的数组

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”最大值

价格/ w0.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| 

这将从数组中找到最低值:

<?php

$Arrays = Array ( 
'0' => Array (
  'p' => 150,
  'w' => 120,
),
'1' => Array (
  'p' => 10,
  'w' => 20,
),
'2' => Array (
  'p' => 70,
  'w' => 10,
),
'3' => Array (
  'p' => 100,
  'w' => 45,
),
'4' => Array (
  'p' => 110,
  'w' => 500,
)
);

$Encode = json_encode($Arrays);

$Decoded = json_decode($Encode);



foreach($Decoded as $Item){
$Num[] = $Item->w;

}
echo min($Num); //Will echo 10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM