將已知數組按條件分為若個新數組

來源:互聯網
上載者:User
已知數組:
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<$n; $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            [plant] => DYNP            [get_date] => 2013-09-09            [cust_no] => 12654172            [total] => 100            [snp] => 15            [mount] => 41            [lp_no] => P000000D        )    [2] => Array        (            [po_nume] => 3/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        )    [3] => Array        (            [po_nume] => 4/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        )    [4] => Array        (            [po_nume] => 5/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        )    [5] => Array        (            [po_nume] => 6/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        )    [6] => Array        (            [po_nume] => 7/7            [po_num] => DYNP-770266110-00            [plant] => DYNP            [get_date] => 2013-09-09            [cust_no] => 12654172            [total] => 15            [snp] => 15            [mount] => 41            [lp_no] => P000000D        )    [7] => 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        ))

...
是啊。又是這種問題。
[6] => Array
(
[po_no] => 7/7
[po_num] => DYNP-770266110-00
[plant] => DYNP
[get_date] => 2013-09-09
[cust_no] => 12654172
[total] => 15
)

[7] => Array
(
[po_num] => DYNP-770266110-00
[plant] => DYNP
[get_date] => 2013-09-09
[cust_no] => 12647212
[total] => 60
)

這個能不能作為一項來處理?將原來的數組併到前一個數組中,而不是重新計算$i/$n的序號。變成這樣的形式:

[6] => Array
(
[po_no] => 7/7
[po_num] => DYNP-770266110-00
[plant] => DYNP
[get_date] => 2013-09-09
[cust_no] => 12654172
[total] => 15
)

[7] => Array
(
[po_no] => 7/7
[po_num] => DYNP-770266110-00
[plant] => DYNP
[get_date] => 2013-09-09
[cust_no] => 12647212
[total] => 60 //15+60=75 < 100
)

又來了?

不好意思,發現問過這個問題了,抱歉!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.