Home  >  Article  >  Backend Development  >  数组拆分处理(整数时的处理)解决方案

数组拆分处理(整数时的处理)解决方案

WBOY
WBOYOriginal
2016-06-13 11:43:44880browse

数组拆分处理(整数时的处理)
已知数组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项生成?

Statement:
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