php把整数拆分成数组,数组拆分处理(整数时的处理),该怎么处理
发布日期:2021-06-24 11:22:13 浏览次数:4 分类:技术文章

本文共 1353 字,大约阅读时间需要 4 分钟。

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

已知数组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;

转载地址:https://blog.csdn.net/weixin_32467749/article/details/116415802 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:oracle numlist,oracle sql str2numlist numtabletype
下一篇:导出oracle11g的空表,轻松解决oracle11g 空表不能 exp 导出 的问题。

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月08日 23时10分22秒