数组拆分处理(整数时的处理)

littlebirds 2013-12-27 03:05:27
已知数组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项生成?
...全文
80 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2013-12-27
  • 打赏
  • 举报
回复
$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;
$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
)

)

)

一起混吧 2013-12-27
  • 打赏
  • 举报
回复
想复杂了吧,你想要什么结果,或者可以换一种思路。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧