使用循环逻辑拆分php
你好,我有一个逻辑问题要完成我的测试 这是我的阵列使用循环逻辑拆分php,php,mysql,arrays,Php,Mysql,Arrays,你好,我有一个逻辑问题要完成我的测试 这是我的阵列 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 (
[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>';
这将从数组中找到最低值:
<?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
这将从数组中找到最低值:
<?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
您好,也许您可以向我们展示您使用循环的尝试,这样我们就可以从那里开始,看看其他人是否可以为您提供更好的解决方案。如果您不发布您目前使用的代码,您至少应该发布一个可用的PHP数组,人们可以使用它来为您粉碎一些东西-如果不手动重写数组,打印输出就没有多大用处,也许你可以向我们展示你使用循环的尝试,这样我们就可以从那里开始,看看其他人是否能为你提供更好的解决方案。如果你不发布你目前使用的代码,您至少应该发布一个可用的PHP数组,人们可以使用它来为您粉碎一些东西——如果不手动重写数组,打印输出就没有多大用处