php二维数组求和
Array([0]=>Array([order_num]=>2013031802[cid]=>54[order_ip]=>125.76.225.1[order_amo]=...
Array
(
[0] => Array
(
[order_num] => 2013031802
[cid] => 54
[order_ip] => 125.76.225.1
[order_amo] => 1200
[end_time] => 1366819200
[order_end] => 1394467200
)
[1] => Array
(
[order_num] => 2013041001
[cid] => 54
[order_ip] => 117.34.17.64
[order_amo] => 300
[end_time] => 1366819200
[order_end] => 1394467200
)
[2] => Array
(
[order_num] => 2013041602
[cid] => 57
[order_ip] => 117.34.17.64
[order_amo] => 440
[end_time] => 1366732800
[order_end] => 1395676800
)
[3] => Array
(
[order_num] => 2013041901
[cid] => 58
[order_ip] => 117.34.17.64
[order_amo] => 360
[end_time] => 1394467200
[order_end] => 1396281600
)
[4] => Array
(
[order_num] => 2013041902
[cid] => 59
[order_ip] => 117.34.17.64
[order_amo] => 3000
[end_time] => 1395676800
[order_end] => 1397836800
)
[5] => Array
(
[order_num] => 2013042401
[cid] => 54
[order_ip] =>
[order_amo] => 30000
[end_time] => 1366819200
[order_end] => 1366732800
)
[6] => Array
(
[order_num] => 2013042501
[cid] => 79
[order_ip] => 192.168.1.1
[order_amo] => 2300
[end_time] => 1366819200
[order_end] => 1366819200
)
[7] => Array
(
[order_num] => 2013042502
[cid] => 79
[order_ip] =>
[order_amo] => 10000
[end_time] => 1366819200
[order_end] => 1366819200
)
)
当CID相同的时候把amo加起来,ip拼接起来(ip不相同的时候,ip不为空),写成一个新的数组 展开
(
[0] => Array
(
[order_num] => 2013031802
[cid] => 54
[order_ip] => 125.76.225.1
[order_amo] => 1200
[end_time] => 1366819200
[order_end] => 1394467200
)
[1] => Array
(
[order_num] => 2013041001
[cid] => 54
[order_ip] => 117.34.17.64
[order_amo] => 300
[end_time] => 1366819200
[order_end] => 1394467200
)
[2] => Array
(
[order_num] => 2013041602
[cid] => 57
[order_ip] => 117.34.17.64
[order_amo] => 440
[end_time] => 1366732800
[order_end] => 1395676800
)
[3] => Array
(
[order_num] => 2013041901
[cid] => 58
[order_ip] => 117.34.17.64
[order_amo] => 360
[end_time] => 1394467200
[order_end] => 1396281600
)
[4] => Array
(
[order_num] => 2013041902
[cid] => 59
[order_ip] => 117.34.17.64
[order_amo] => 3000
[end_time] => 1395676800
[order_end] => 1397836800
)
[5] => Array
(
[order_num] => 2013042401
[cid] => 54
[order_ip] =>
[order_amo] => 30000
[end_time] => 1366819200
[order_end] => 1366732800
)
[6] => Array
(
[order_num] => 2013042501
[cid] => 79
[order_ip] => 192.168.1.1
[order_amo] => 2300
[end_time] => 1366819200
[order_end] => 1366819200
)
[7] => Array
(
[order_num] => 2013042502
[cid] => 79
[order_ip] =>
[order_amo] => 10000
[end_time] => 1366819200
[order_end] => 1366819200
)
)
当CID相同的时候把amo加起来,ip拼接起来(ip不相同的时候,ip不为空),写成一个新的数组 展开
1个回答
展开全部
$new = array();
foreach($array as $value){
//新的cid号存一个新的数组
if(empty($new[$value['cid']])){
$new[$value['cid']] = $value;
//存储IP进入数组,方便后续判断
$new[$value['cid']]['ip'] = array($value['order_ip']);
}else{
//累加amo的值
$new[$value['cid']]['order_amo'] += $value['order_amo'];
//判断IP是否为空或者是否重复
if(!empty($value['order_ip']) && !in_array($value['order_ip'],$new[$value['cid']])){
//拼接ip,并添加到IP数组,方便后续判断
$new[$value['cid']]['order_ip'] .= ','.$value['order_ip'];
$new[$value['cid']]['ip'] = array($value['order_ip']);
}
}
}
foreach($array as $value){
//新的cid号存一个新的数组
if(empty($new[$value['cid']])){
$new[$value['cid']] = $value;
//存储IP进入数组,方便后续判断
$new[$value['cid']]['ip'] = array($value['order_ip']);
}else{
//累加amo的值
$new[$value['cid']]['order_amo'] += $value['order_amo'];
//判断IP是否为空或者是否重复
if(!empty($value['order_ip']) && !in_array($value['order_ip'],$new[$value['cid']])){
//拼接ip,并添加到IP数组,方便后续判断
$new[$value['cid']]['order_ip'] .= ','.$value['order_ip'];
$new[$value['cid']]['ip'] = array($value['order_ip']);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询