将已知数组按条件分为若个新数组

php中文网
发布: 2016-06-23 14:24:47
原创
999人浏览过

已知数组:

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          )

又来了?

不好意思,发现问过这个问题了,抱歉!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号