数组针对某项的求和问题
已知数组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);
(
[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',
如果是去重复求和呢?
去重复求和 抬头看看一楼....

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、
