Home > Article > Backend Development > 数组拆分问题
假设数组a:
array ( 0 => array ( 'po_num' => 'DYNP-770266110-00', 'plant' => 'DYNP', 'get_date' => '2013-09-09', 'cust_no' => '12647212', 'total' => '60', 'snp' => '15', 'mount' => '4', 'lp_no' => 'P000000D', ), 1 => array ( 'po_num' => 'DYNP-770266110-00', 'plant' => 'DYNP', 'get_date' => '2013-09-09', 'cust_no' => '12654172', 'total' => '615', 'snp' => '15', 'mount' => '41', 'lp_no' => 'P000000D', ),)
//barray ( 0 => array ( 'po_num' => 'DYNP-770266110-00', 'plant' => 'DYNP', 'get_date' => '2013-09-09', 'cust_no' => '12647212', 'total' => '60', 'snp' => '15', 'mount' => '4', 'lp_no' => 'P000000D', ), 1 => array ( 'po_num' => 'DYNP-770266110-00', 'plant' => 'DYNP', 'get_date' => '2013-09-09', 'cust_no' => '12654172', 'total' => '165', 'snp' => '15', 'mount' => '11', 'lp_no' => 'P000000D', ),)//carray ( 0 => array ( 'po_num' => 'DYNP-770266110-00', 'plant' => 'DYNP', 'get_date' => '2013-09-09', 'cust_no' => '12647212', 'total' => '225', 'snp' => '15', 'mount' => '15', 'lp_no' => 'P000000D', ),)//darray ( 0 => array ( 'po_num' => 'DYNP-770266110-00', 'plant' => 'DYNP', 'get_date' => '2013-09-09', 'cust_no' => '12647212', 'total' => '225', 'snp' => '15', 'mount' => '15', 'lp_no' => 'P000000D', ),)
$a = array ( 0 => array ( 'po_num' => 'DYNP-770266110-00', 'plant' => 'DYNP', 'get_date' => '2013-09-09', 'cust_no' => '12647212', 'total' => '60', 'snp' => '15', 'mount' => '4', 'lp_no' => 'P000000D', ), 1 => array ( 'po_num' => 'DYNP-770266110-00', 'plant' => 'DYNP', 'get_date' => '2013-09-09', 'cust_no' => '12654172', 'total' => '615', 'snp' => '15', 'mount' => '41', 'lp_no' => 'P000000D', ),);$t = 225;foreach($a as $v) { $t1 = $v['total']; $v['total'] = $t1 % $t; $res[] = $v; $t1 -= $v['total']; while($t1 >= $t) { $v['total'] = $t; $t1 -= $t; $res[] = $v; }}print_r($res);Array
...
能不能变成这样?把3组区分出来。
1楼
如果mount项的值也发生改变,是不是和这个一样?
$t = 225;foreach($a as $v) { $t1 = $v['total']; $v['total'] = $t1 % $t; $res[] = $v; $t1 -= $v['total']; while($t1 >= $t) { $v['total'] = $t; $t1 -= $t; $res[] = $v; }}print_r($res);
Array([0]=>Array( [0] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12647212 [total] => 60 [snp] => 15 [mount] => 4 [lp_no] => P000000D ) [1] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 165 [snp] => 15 [mount] => 41 [lp_no] => P000000D ))[1] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 225 [snp] => 15 [mount] => 41 [lp_no] => P000000D )[2] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 225 [snp] => 15 [mount] => 41 [lp_no] => P000000D ))
当然可以,不过有什么意义呢?
$a = array ( 0 => array ( 'po_num' => 'DYNP-770266110-00', 'plant' => 'DYNP', 'get_date' => '2013-09-09', 'cust_no' => '12647212', 'total' => '60', 'snp' => '15', 'mount' => '4', 'lp_no' => 'P000000D', ), 1 => array ( 'po_num' => 'DYNP-770266110-00', 'plant' => 'DYNP', 'get_date' => '2013-09-09', 'cust_no' => '12654172', 'total' => '615', 'snp' => '15', 'mount' => '41', '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; }}print_r($res);Array
当然可以,不过有什么意义呢?
我是想记循环输出的结果,foreach($res1 as $value)这样。
当然可以,不过有什么意义呢?
我用了array_chunk,怎么结果变成了这样?
Array( [0] => Array ( [0] => Array ( [0] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12647212 [total] => 60 [snp] => 15 [mount] => 4 [lp_no] => P000000D ) [1] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 165 [snp] => 15 [mount] => 11 [lp_no] => P000000D ) ) ) [1] => Array ( [0] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 225 [snp] => 15 [mount] => 15 [lp_no] => P000000D ) ) [2] => Array ( [0] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 225 [snp] => 15 [mount] => 15 [lp_no] => P000000D ) ))
Array( [0] => Array ( [0] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12647212 [total] => 60 [snp] => 15 [mount] => 4 [lp_no] => P000000D ) [1] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 165 [snp] => 15 [mount] => 11 [lp_no] => P000000D ) ) [1] => Array ( [0] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 225 [snp] => 15 [mount] => 15 [lp_no] => P000000D ) ) [2] => Array ( [0] => Array ( [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 225 [snp] => 15 [mount] => 15 [lp_no] => P000000D ) ))
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;
}
}