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

WBOY
Release: 2016-06-13 12:30:44
Original
744 people have browsed it

数组拆分处理(整数时的处理)
已知数组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 />
)
Copy after login


处理程序如下:
<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 />
Copy after login


得到的结果:
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 />
)
Copy after login


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

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