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

WBOY
Lepaskan: 2016-06-23 14:40:13
asal
1051 orang telah melayarinya

已知数组a:

array (  0 =>   array (    'po_num' => '20131227-000008102',    'plant' => 'JQSP',    'get_date' => '2013-12-27',    'cust_no' => '12654172',    'total' => '225',    'snp' => '15',    'mount' => '15',    'lp_no' => 'P000000D',  ),)
Salin selepas log masuk


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


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


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


回复讨论(解决方案)

想复杂了吧,你想要什么结果,或者可以换一种思路。

本帖最后由 xuzuning 于 2013-12-27 15:34:48 编辑

$a = array (  0 =>   array (    'po_num' => '20131227-000008102',    'plant' => 'JQSP',    'get_date' => '2013-12-27',    'cust_no' => '12654172',    'total' => '225',    'snp' => '15',    'mount' => '15',    'lp_no' => 'P000000D',  ),);$t = 225;$k = 0; foreach($a as $v) {  $t1 = $v['total'];  if($t1 % $t) $v['total'] = $t1 % $t;  $res[$k][] = $v;  $t1 -= $v['total'];  while($t1 >= $t) {    $v['total'] = $t;    $t1 -= $t;    $res[++$k][] = $v;  }}$res = array_reverse($res);print_r($res);
Salin selepas log masuk
Array(    [0] => Array        (            [0] => Array                (                    [po_num] => 20131227-000008102                    [plant] => JQSP                    [get_date] => 2013-12-27                    [cust_no] => 12654172                    [total] => 225                    [snp] => 15                    [mount] => 15                    [lp_no] => P000000D                )        ))
Salin selepas log masuk

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan