0

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)
);
Taz
  • 3,718
  • 2
  • 37
  • 59
Wenuka
  • 887
  • 2
  • 9
  • 25
  • 2
    Please update your question to include any attempts you've (and errors with those attempts) made in order to achieve the desired result – Epodax Aug 19 '16 at 09:54
  • @Epodax, Sure thing, I'll make sure to do do it in the next question. Sorry, I don't have the previous attempts because I already replaced with the given answers. Thanks anyway for the incremental feedback. – Wenuka Sep 02 '16 at 05:42

3 Answers3

1
<?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)
);

$place_array = array();
foreach ($array1 as $key => $value) {
    if(isset($place_array[$value['location']]))
        $place_array[$value['location']]['time_spent'] += $value['time_spent'];
    else
        $place_array[$value['location']] = $value;
}
$place_array = array_values($place_array);
echo '<pre>';
print_r($place_array);
echo '</pre>';
?>

Output

Array
(
    [0] => Array
    (
        [location] => AA
        [time_spent] => 6
    )

    [1] => Array
    (
        [location] => BB
        [time_spent] => 2
    )

    [2] => Array
    (
        [location] => CC
        [time_spent] => 4
    )
)
Shri Suresh
  • 463
  • 1
  • 5
  • 20
1

You can try this one.

I have created a new array $result with its key as location say AA and in this I have save array which have location as AA and its time-spent next time I am checking if location is AA then add its time-spent to previous and if not then add new. At the last I used array_values to changed keys.

    <?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));
    $result = array();
    foreach($array1 as $new){
    if (array_key_exists($new['location'],$result)){
        $result[$new['location']]['time_spent'] += $new['time_spent'];
        }
    else{
        $result[$new['location']] = $new;
        }
    }
    $final = $new_array=array_values($result);
    echo "<pre>";print_r($final);

Output

    Array
    (
        [0] => Array
            (
                [location] => AA
                [time_spent] => 6
            )
    
        [1] => Array
            (
                [location] => BB
                [time_spent] => 2
            )
    
        [2] => Array
            (
                [location] => CC
                [time_spent] => 4
            )
    
    )
halfer
  • 19,824
  • 17
  • 99
  • 186
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
1

My answer shows you how I'd get the array you wanted but also the actual array I would use if it was me coding. If all I wanted was a summary of locations and total time spent I'd deal with a more compact array that didn't need to keep storing the labels. Anyway, if you don't want the summary bit, just remove it if you think this version is more compact :)

    $journey = 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)
);

$summary = Array();
$full    = Array();
foreach($journey as $map){

    // Your version
    if(!isset($full[$map["location"]])){
        $full[$map["location"]] = $map;
    } else {
        $full[$map["location"]]["time_spent"] += $map["time_spent"];
    }

    // Summary version (leaner)
    $summary[$map["location"]] = (isset($summary[$map["location"]])?$summary[$map["location"]]:0)+$map["time_spent"];
}

// Summary result
print "<pre>";
print_r($summary);
print "</pre>";

// Your result
print "<pre>";
print_r(array_values($full));
print "</pre>";

Output

Array
(
    [AA] => 6
    [BB] => 2
    [CC] => 4
)

Array
(
    [0] => Array
        (
            [location] => AA
            [time_spent] => 6
        )

    [1] => Array
        (
            [location] => BB
            [time_spent] => 2
        )

    [2] => Array
        (
            [location] => CC
            [time_spent] => 4
        )

)
Watts Epherson
  • 692
  • 5
  • 9