请PHP参考下面的数组:如何在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)
);
在这里我要计算他在每一个具体的位置花费的总时间,即积累的关键time_spent
值如果location
关键是一样的。然后将其记录在一个新的关联数组中(如$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
@Epodax,当然,我会确保在下一个问题中做到这一点。对不起,我没有以前的尝试,因为我已经替换了给定的答案。无论如何感谢增量反馈。 – Wenuka