数组针对某项的求和问题
已知数组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',
),
)
[解决办法]
$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));
$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);
)
[310F61VA5B] => Array
(
[0] => CVT121015002
[1] => CVT121226001
)
)
Array
(
[SU13080800340] => Array
(
[0] => 1
[1] => 2
[2] => 2
)
)
Array
(
[SU13080800340] => Array
(
[0] => 16
[1] => 32
[2] => 32
)
)
[解决办法]
$a = array (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);
[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
)
)
)
[解决办法]
楼主这种问题 应该算是比较基础的 我有点好奇为什么会纠结于
Array
(
[0] => 16
[1] => 32
[2] => 32
)
这种数组的加和? 你的需求应该是去了重复求和.... 一个PHP函数的事情...