当前位置:Gxlcms > PHP教程 > 数组求和操作,该怎么处理

数组求和操作,该怎么处理

时间:2021-07-01 10:21:17 帮助过:7人阅读

数组求和操作

foreach($a as $k=>$v)
{
print_r($newarr);
echo $a[$k]['5'].'
';
}



Array
(
[0] => Array
(
[lotno] => 3206
[count] => 12
)

[1] => Array
(
[lotno] => 3207
[count] => 63
)

[2] => Array
(
[lotno] => 3218
[count] => 180
)

)
120
Array
(
[0] => Array
(
[lotno] => 3206
[count] => 12
)

[1] => Array
(
[lotno] => 3207
[count] => 63
)

[2] => Array
(
[lotno] => 3218
[count] => 180
)

)
135


将count相加,如果对应行的结果为120,则显示3206*12 3207*63 3218*45
若对应行的结果为135,则显示3218*135 (135=180-45(上一轮count已用的45),不够则补齐)

分享到:


------解决方案--------------------
这个意思
$ar = array (
array( 'lotno' => 3206, 'count' => 12 ),
array( 'lotno' => 3207, 'count' => 63 ),
array( 'lotno' => 3218, 'count' => 180 ),
);

$s = '';
$n = 0;
foreach($ar as $r) {
if($n + $r['count'] > 120) {
echo $s . $r['lotno'] . '*' . (120-$n) ."\n";
echo $r['lotno'] . '*' . ($n = $r['count'] - (120-$n)) ."\n";

人气教程排行