首页 >后端开发 >php教程 > 数组拆分处置(整数时的处理)

数组拆分处置(整数时的处理)

WBOY
WBOY原创
2016-06-13 12:30:44777浏览

数组拆分处理(整数时的处理)
已知数组a:

array (<br />
  0 => <br />
  array (<br />
    'po_num' => '20131227-000008102',<br />
    'plant' => 'JQSP',<br />
    'get_date' => '2013-12-27',<br />
    'cust_no' => '12654172',<br />
    'total' => '225',<br />
    'snp' => '15',<br />
    'mount' => '15',<br />
    'lp_no' => 'P000000D',<br />
  ),<br />
)


处理程序如下:
<br />
$t = 225;<br />
$k = 0;<br />
<br />
foreach($a as $v) {<br />
$t1 = $v['total'];<br />
$v['total'] = $t1 % $t;<br />
$res[$k][] = $v;<br />
$t1 -= $v['total'];<br />
while($t1 >= $t) {<br />
$v['total'] = $t;<br />
$t1 -= $t;<br />
$res[++$k][] = $v;<br />
	}<br />
	}<br />
$res = array_reverse($res);<br />
print_r($res);<br />


得到的结果:
Array<br />
(<br />
    [0] => Array<br />
        (<br />
            [0] => Array<br />
                (<br />
                    [po_num] => 20131227-000008102<br />
                    [plant] => JQSP<br />
                    [get_date] => 2013-12-27<br />
                    [cust_no] => 12654172<br />
                    [total] => 225<br />
                    [snp] => 15<br />
                    [mount] => 15<br />
                    [lp_no] => P000000D<br />
                )<br />
<br />
        )<br />
<br />
    [1] => Array<br />
        (<br />
            [0] => Array<br />
                (<br />
                    [po_num] => 20131227-000008102<br />
                    [plant] => JQSP<br />
                    [get_date] => 2013-12-27<br />
                    [cust_no] => 12654172<br />
                    [total] => 0 //产生了为0的项<br />
                    [snp] => 15<br />
                    [mount] => 15<br />
                    [lp_no] => P000000D<br />
                )<br />
<br />
        )<br />
<br />
)


既然是整除,那么结果应该只有一项,如何修改代码让其只有非0项生成?

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn