請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