数组拆分问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了数组拆分问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含10391字,纯文字阅读大概需要15分钟。
内容图文
假设数组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', ),)
当满足整个total为一定条件时,将数组拆分开来。例中total为675,假设拆分条件为total满足225,也就是拆分为3个数组,如何能将数组拆分成这样?
//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
(
[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
)
[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
)
[3] => 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
)
)
...
能不能变成这样?把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
(
[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
)
)
当然可以,不过有什么意义呢?
我是想记循环输出的结果,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;
}
}
内容总结
以上是互联网集市为您收集整理的数组拆分问题全部内容,希望文章能够帮你解决数组拆分问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。