Array sharding (integer processing)

Source: Internet
Author: User
Array sharding (processing of integers) known array:
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',  ),)


The processing procedure is as follows:
$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);


Result:
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 // items with 0 are generated [snp] => 15 [mount] => 15 [lp_no] => P000000D )))


Since the division is complete, there should be only one result. how can I modify the code to make it generate only non-zero items?


Reply to discussion (solution)

It's complicated to think about, what results do you want, or you can change your mind.

This post was last edited by xuzuning at 15:34:48 on

$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                )        ))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.