已知數組a:
array ( 0 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5A', 'mount_total' => '1', 'total' => '16', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121015001', ), 1 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5B', 'mount_total' => '2', 'total' => '32', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121015002', ), 2 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5B', 'mount_total' => '2', 'total' => '32', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121226001', ),)
如何分別求得數組中的項cust_no對應的packageno的結果以及stock_no對應mount_total和total項求和的結果?
假設cust_no為310F61VA5A時,對應的packageno為CVT121225001。
cust_no為310F61VA5B時,對應的packageno為CVT121225002,CVT121226001。
stock_no為SU13080800340時,求得的mount_total結果為3(1+2),total結果為48(16+32)。
如何達到這樣的效果?
回複討論(解決方案)
$a = array ( 0 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5A', 'mount_total' => '1', 'total' => '16', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121015001', ), 1 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5B', 'mount_total' => '2', 'total' => '32', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121015002', ), 2 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5B', 'mount_total' => '2', 'total' => '32', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121226001', ), );function foo1($cust_no,$a=array()){foreach($a as $each){if($each['cust_no'] == $cust_no){$res[] = $each['packageno'];}}if($res){return implode(",",$res);}else{return false;}}function foo2($stock_no,$a=array()){foreach($a as $each){if($each['stock_no'] == $stock_no){$mount_total[] = $each['mount_total'];$total[] = $each['total'];}}$mount_total = array_unique($mount_total);//去重複$total = array_unique($total);$res['mount_total'] = array_sum($mount_total);$res['total'] = array_sum($total);return $res;}var_export(foo1("310F61VA5B",$a));var_export(foo2("SU13080800340",$a));
'CVT121015002,CVT121226001'
array ( 'mount_total' => 3, 'total' => 48, )
$ar=array ( 0 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5A', 'mount_total' => '1', 'total' => '16', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121015001', ), 1 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5B', 'mount_total' => '2', 'total' => '32', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121015002', ), 2 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5B', 'mount_total' => '2', 'total' => '32', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121226001', ),);foreach($ar as $v){ $a[$v['cust_no']][]=$v['packageno'];$b[$v['stock_no']][]=$v['mount_total'];$c[$v['stock_no']][]=$v['total'];}print_r($a);print_r($b);print_r($c);
Array
(
[310F61VA5A] => Array
(
[0] => CVT121015001
)
[310F61VA5B] => Array
(
[0] => CVT121015002
[1] => CVT121226001
)
)
Array
(
[SU13080800340] => Array
(
[0] => 1
[1] => 2
[2] => 2
)
)
Array
(
[SU13080800340] => Array
(
[0] => 16
[1] => 32
[2] => 32
)
)
第一個問題 迴圈數組 判斷每一個cust_no欄位的值為給定值的數組元素的packageno的值加起來 就是你要的結果.
第二個問題 迴圈數組 判斷每一個stock_no欄位的值為給定值的數組元素的兩個要求欄位的值存入數組,迴圈結束後分別去除重複的然後相加
$a = array ( 0 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5A', 'mount_total' => '1', 'total' => '16', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121015001', ), 1 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5B', 'mount_total' => '2', 'total' => '32', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121015002', ), 2 => array ( 'stock_no' => 'SU13080800340', 'adress' => 'B', 'arr_time' => '2013-08-14 09:00:00', 'c_type' => 'P32E', 'cust_no' => '310F61VA5B', 'mount_total' => '2', 'total' => '32', 'c1_time' => '2013-08-10 15:00:00', 'packageno' => 'CVT121226001', ),);$res = array();foreach($a as $item) { if(! isset($res[$item['stock_no']][$item['cust_no']])) { $res[$item['stock_no']][$item['cust_no']] = $item; }else { $res[$item['stock_no']][$item['cust_no']]['mount_total'] += $item['mount_total']; $res[$item['stock_no']][$item['cust_no']]['total'] += $item['total']; $res[$item['stock_no']][$item['cust_no']]['packageno'] .= ',' .$item['packageno']; }}foreach($res as &$item) $item = array_values($item);print_r($res);Array
(
[SU13080800340] => Array
(
[0] => Array
(
[stock_no] => SU13080800340
[adress] => B
[arr_time] => 2013-08-14 09:00:00
[c_type] => P32E
[cust_no] => 310F61VA5A
[mount_total] => 1
[total] => 16
[c1_time] => 2013-08-10 15:00:00
[packageno] => CVT121015001
)
[1] => Array
(
[stock_no] => SU13080800340
[adress] => B
[arr_time] => 2013-08-14 09:00:00
[c_type] => P32E
[cust_no] => 310F61VA5B
[mount_total] => 4
[total] => 64
[c1_time] => 2013-08-10 15:00:00
[packageno] => CVT121015002,CVT121226001
)
)
)
[code=php]
Array
(
[310F61VA5A] => Array
(
[0] => CVT121015001
)
[310F61VA5B] => Array
(
[0] => CVT121015002
[1] => CVT121226001
)
)
Array
(
[SU13080800340] => Array
(
[0] => 1
[1] => 2
[2] => 2
)
)
Array
(
[SU13080800340] => Array
(
[0] => 16
[1] => 32
[2] => 32
)
)
第一項OK,但是mount_total和total的求和結果不對,我要的是stock_no為SU13080800340時,求得的mount_total結果為3(1+2),total結果為48(16+32)。
$res = array();foreach($a as $item) { if(! isset($res[$item['stock_no']][$item['cust_no']])) { $res[$item['stock_no']][$item['cust_no']] = $item; }else { $res[$item['stock_no']][$item['cust_no']]['mount_total'] += $item['mount_total']; $res[$item['stock_no']][$item['cust_no']]['total'] += $item['total']; $res[$item['stock_no']][$item['cust_no']]['packageno'] .= ',' .$item['packageno']; }}foreach($res as &$item) $item = array_values($item);print_r($res);
我想要得到的stockno的求和結果是mount_total為3(2+1),total結果為48(16+32)。
樓主這種問題 應該算是比較基礎的 我有點好奇為什麼會糾結於
Array
(
[0] => 16
[1] => 32
[2] => 32
)
這種數組的加和? 你的需求應該是去了重複求和.... 一個PHP函數的事情...
你的數組中三個stock_no的值不是一樣的嗎,都為 SU13080800340 吧,難道我眼花了?
那是你的資料給錯了
'cust_no' => '310F61VA5A',
'mount_total' => '1',
'total' => '16',
'cust_no' => '310F61VA5B',
'mount_total' => '2',
'total' => '32',
'cust_no' => '310F61VA5B',
'mount_total' => '2',
'total' => '32',
那是你的資料給錯了
'cust_no' => '310F61VA5A',
'mount_total' => '1',
'total' => '16',
'cust_no' => '310F61VA5B',
'mount_total' => '2',
'total' => '32',
'cust_no' => '310F61VA5B',
'mount_total' => '2',
'total' => '32',
那我再想想。
你的數組中三個stock_no的值不是一樣的嗎,都為 SU13080800340 吧,難道我眼花了?
那怎麼去重複求和呢?
那是你的資料給錯了
'cust_no' => '310F61VA5A',
'mount_total' => '1',
'total' => '16',
'cust_no' => '310F61VA5B',
'mount_total' => '2',
'total' => '32',
'cust_no' => '310F61VA5B',
'mount_total' => '2',
'total' => '32',
如果是去重複求和呢?
那是你的資料給錯了
'cust_no' => '310F61VA5A',
'mount_total' => '1',
'total' => '16',
'cust_no' => '310F61VA5B',
'mount_total' => '2',
'total' => '32',
'cust_no' => '310F61VA5B',
'mount_total' => '2',
'total' => '32',
如果是去重複求和呢?
去重複求和 抬頭看看一樓....