ホームページ >バックエンド開発 >PHPチュートリアル >配列の分割処理(整数の処理)
既知の配列 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'];$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);
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 ) ))
それは複雑です。どのような結果が必要ですか、あるいは考えを変えることもできます。
$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);
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 ) ))