foreach($a as $k=>$v){ print_r($newarr); echo $a[$k]['5'].'<br />';}
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
看不懂你的意思,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
$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) .'<br>'; } else if($v['5']==135){ echo $end['lotno'] ."*" .($end['count']-$sub); }}