【发布时间】:2016-12-26 10:29:41
【问题描述】:
请参考PHP中的以下数组:
$array1 = array(
array('location'=>'AA','time_spent'=>2),
array('location'=>'BB','time_spent'=>2),
array('location'=>'AA','time_spent'=>3),
array('location'=>'CC','time_spent'=>2),
array('location'=>'CC','time_spent'=>2),
array('location'=>'AA','time_spent'=>1)
);
在这里,我想计算他在每个特定位置花费的总时间,即如果 location 的键相同,则累积键 time_spent 的值。然后将其记录在一个新的关联数组中(比如$place_array),其值与键location 和total_time 相关联。
所以输出数组应该是
$place_array = array(
array('location'=>'AA','time_spent'=>6),
array('location'=>'BB','time_spent'=>2),
array('location'=>'CC','time_spent'=>4)
);
【问题讨论】:
-
请更新您的问题,以包括您为达到预期结果所做的任何尝试(以及这些尝试的错误)
-
@Epodax,当然,我会确保在下一个问题中做到这一点。抱歉,我没有以前的尝试,因为我已经用给定的答案替换了。无论如何,感谢您的增量反馈。
标签: php key associative-array accumulate