首页 / PHP / 将已知数组按条件分为若个新数组
将已知数组按条件分为若个新数组
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了将已知数组按条件分为若个新数组,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5130字,纯文字阅读大概需要8分钟。
内容图文
![将已知数组按条件分为若个新数组](/upload/InfoBanner/zyjiaocheng/195/05b2c52464d54f879dfd2c0b92744aca.jpg)
array ( 0 => 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', ), 1 => 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', ),)
能否按字段total为100为单位把数组再分为若干个新的数组?并加上序号字段在其中,比如:
array ( 'po_num' => '1/7',//新增字段信息 'po_num' => 'DYNP-770266110-00', 'plant' => 'DYNP', 'get_date' => '2013-09-09', 'cust_no' => '12654172', 'total' => '615', 'snp' => '15', 'mount' => '41', 'lp_no' => 'P000000D', ),...)
回复讨论(解决方案)
需求不明确~~
又来了?
$ar = array ( 0 => 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', ), 1 => 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', ),);$split_num = 100;foreach($ar as $item) { if($item['total'] <= $split_num) { $res[] = $item; continue; } $total = $item['total']; $n = ceil($total/$split_num); for($i=1; $i<$n; $i++) { $res[] = array_merge(array('po_nume' => "$i/$n"), $item, array('total' => $split_num)); } $res[] = array_merge(array('po_nume' => "$i/$n"), $item, array('total' => $total%$split_num));}print_r($res);
Array( [0] => Array ( [po_nume] => 1/7 [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 100 [snp] => 15 [mount] => 41 [lp_no] => P000000D ) [1] => Array ( [po_nume] => 2/7 [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 100 [snp] => 15 [mount] => 41 [lp_no] => P000000D ) [2] => Array ( [po_nume] => 3/7 [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 100 [snp] => 15 [mount] => 41 [lp_no] => P000000D ) [3] => Array ( [po_nume] => 4/7 [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 100 [snp] => 15 [mount] => 41 [lp_no] => P000000D ) [4] => Array ( [po_nume] => 5/7 [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 100 [snp] => 15 [mount] => 41 [lp_no] => P000000D ) [5] => Array ( [po_nume] => 6/7 [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 100 [snp] => 15 [mount] => 41 [lp_no] => P000000D ) [6] => Array ( [po_nume] => 7/7 [po_num] => DYNP-770266110-00 [plant] => DYNP [get_date] => 2013-09-09 [cust_no] => 12654172 [total] => 15 [snp] => 15 [mount] => 41 [lp_no] => P000000D ) [7] => 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 ))
...
是啊。又是这种问题。
[6] => Array
(
[po_no] => 7/7
[po_num] => DYNP-770266110-00
[plant] => DYNP
[get_date] => 2013-09-09
[cust_no] => 12654172
[total] => 15
)
[7] => Array
(
[po_num] => DYNP-770266110-00
[plant] => DYNP
[get_date] => 2013-09-09
[cust_no] => 12647212
[total] => 60
)
这个能不能作为一项来处理?将原来的数组并到前一个数组中,而不是重新计算$i/$n的序列号。变成这样的形式:
[6] => Array
(
[po_no] => 7/7
[po_num] => DYNP-770266110-00
[plant] => DYNP
[get_date] => 2013-09-09
[cust_no] => 12654172
[total] => 15
)
[7] => Array
(
[po_no] => 7/7
[po_num] => DYNP-770266110-00
[plant] => DYNP
[get_date] => 2013-09-09
[cust_no] => 12647212
[total] => 60 //15+60=75 < 100
)
又来了?
不好意思,发现问过这个问题了,抱歉!
内容总结
以上是互联网集市为您收集整理的将已知数组按条件分为若个新数组全部内容,希望文章能够帮你解决将已知数组按条件分为若个新数组所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。