php二维数组求和
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php二维数组求和相关的知识,希望对你有一定的参考价值。
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不为空),写成一个新的数组
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']);
本回答被提问者采纳
求助:PHP如何判断二维数组哪些竖列的值相同?
例子:
M[0][0] M[0][1] M[0][2]
M[1][0] M[1][1] M[1][2]
M[2][0] M[2][1] M[2][2]
$data[$i][$j] 数组一个很大的。知道$i和$j的最大值
以上输出
000
010
000
如何判断上面那些竖列的值都为0?
$m = array(
array(0, 0, 0),
array(0, 1, 0),
array(0, 0, 0),
);
$max_i = 3;
$max_j = 3;
for ($column=0; $column!=$max_j; $column++)
$result = true;
for ($row=0; $row!=$max_i; $row++)
if ($m[$row][$column] != 0)
$result = false;
break;
echo "第$column列".($result ? '全' : '不全')."为空\\n";
输出:
第0列全为空
第1列不全为空
第2列全为空
参考技术A 竖列???就是M[0][0]、M[1][0]、M[2][0]。。。。。。的值呗$count = $i的最大值
for($i=0; $i++; $<$count)
echo M[$i][0];
echo '<br />';
追问
for($i = 0; $i<$count; $i++)
for($j = 0; $j < $count2;$j++)
#需要的是判断M[0][0]、M[1][0]、M[2][0] / M[0][1]、M[1][1]、M[2][1] / M[0][2]、M[1][2]、M[2][2]是值否相同
以上是关于php二维数组求和的主要内容,如果未能解决你的问题,请参考以下文章