数组求和操作
程序员文章站
2022-03-17 19:46:41
...
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 ))120Array( [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),不够则补齐)
回复讨论(解决方案)
看不懂你的意思,45 是写死的还是算出来的? 贴出你的最后结果吧。
算出来的。45 = 120-12-63。
结果类似于:
数量 对应的项
120 3206*12 3207*63 3208*45
135 3208*135(135 = 180-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"; $s = ''; }else { $s .= "$r[lotno]*$r[count] "; $n += $r['count']; if($n == 120) { echo "$s\n"; $s = ''; $n = 0; } }}3206*12 3207*63 3218*45
3218*135
$ar = array ( array( 'lotno' => 3206, 'count' => 12 ), array( 'lotno' => 3207, 'count' => 63 ), array( 'lotno' => 3218, 'count' => 180 ),);$a=array( array('5'=>120), array('5'=>135));foreach($a as $k=>$v){ echo $v['5']." "; if($v['5']==120){ $c=0; foreach(array_slice($ar,0,-1) as $vl){ $c+=$vl['count']; echo $vl['lotno']."*".$vl['count'] ." "; } $end=end($ar); echo $end['lotno'] ."*" . ($sub=$v['5']-$c) .'
'; } else if($v['5']==135){ echo $end['lotno'] ."*" .($end['count']-$sub); }}
上一篇: 这个是pdo的bug吗?