Please refer following array in 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)
);
Here I want to calculate total time he spent in each specific location, i.e to accumulate value of key time_spent
if the key for location
is same. And then to record it in a new associative array (say $place_array
) with values associate to keys location
and total_time
.
So the output array should be
$place_array = array(
array('location'=>'AA','time_spent'=>6),
array('location'=>'BB','time_spent'=>2),
array('location'=>'CC','time_spent'=>4)
);