欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

数组拆分处理(整数时的处理),该怎么处理

程序员文章站 2022-06-15 12:50:10
...
数组拆分处理(整数时的处理)
已知数组a:
array (
0 =>
array (
'po_num' => '20131227-000008102',
'plant' => 'JQSP',
'get_date' => '2013-12-27',
'cust_no' => '12654172',
'total' => '225',
'snp' => '15',
'mount' => '15',
'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;
}
}
$res = array_reverse($res);
print_r($res);


得到的结果:
Array
(
[0] => Array
(
[0] => Array
(
[po_num] => 20131227-000008102
[plant] => JQSP
[get_date] => 2013-12-27
[cust_no] => 12654172
[total] => 225
[snp] => 15
[mount] => 15
[lp_no] => P000000D
)

)

[1] => Array
(
[0] => Array
(
[po_num] => 20131227-000008102
[plant] => JQSP
[get_date] => 2013-12-27
[cust_no] => 12654172
[total] => 0 //产生了为0的项
[snp] => 15
[mount] => 15
[lp_no] => P000000D
)

)

)


既然是整除,那么结果应该只有一项,如何修改代码让其只有非0项生成?
------解决方案--------------------
本帖最后由 xuzuning 于 2013-12-27 15:34:48 编辑
$a = array (
0 =>
array (
'po_num' => '20131227-000008102',
'plant' => 'JQSP',
'get_date' => '2013-12-27',
'cust_no' => '12654172',
'total' => '225',
'snp' => '15',
'mount' => '15',
'lp_no' => 'P000000D',
),
);
$t = 225;
$k = 0;

foreach($a as $v) {
$t1 = $v['total'];
if($t1 % $t) $v['total'] = $t1 % $t;
$res[$k][] = $v;
$t1 -= $v['total'];
while($t1 >= $t) {
$v['total'] = $t;
数组拆分处理(整数时的处理),该怎么处理

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频


网友评论

文明上网理性发言,请遵守 新闻评论服务协议

我要评论
  • 数组拆分处理(整数时的处理),该怎么处理
  • 专题推荐