Home > Backend Development > PHP Tutorial > 数组求和操作

数组求和操作

WBOY
Release: 2016-06-23 14:12:28
Original
1462 people have browsed it

foreach($a as $k=>$v){	print_r($newarr);	echo $a[$k]['5'].'<br />';}
Copy after login


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
Copy after login


将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;    }  }}
Copy after login
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) .'<br>';	}	else if($v['5']==135){	     echo $end['lotno'] ."*" .($end['count']-$sub);	}}
Copy after login
 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template