php 数组根据条件拆分成多个数组,将已知数组按条件分成若个新数组

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

已知数组:

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

$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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值