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)
);
4

3 に答える 3

1

私の答えは、あなたが望む配列を取得する方法だけでなく、私がコーディングした場合に使用する実際の配列も示しています。場所と費やした合計時間の要約だけが必要な場合は、ラベルを保存し続ける必要のない、よりコンパクトな配列を処理します。とにかく、要約ビットが必要ない場合は、このバージョンの方がコンパクトだと思う場合は削除してください:)

    $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>";

出力

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
        )

)
于 2016-08-19T10:29:23.463 に答える
1

これを試すことができます。

$result私はそのキーを場所として新しい配列を作成しました。これには、場所が次の場所とその時間-費やさAAれた場所を持つ配列が保存されています。. 最後に、私はキーを変更していました。AAAAarray_values

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

出力

    Array
    (
        [0] => Array
            (
                [location] => AA
                [time_spent] => 6
            )
    
        [1] => Array
            (
                [location] => BB
                [time_spent] => 2
            )
    
        [2] => Array
            (
                [location] => CC
                [time_spent] => 4
            )
    
    )
于 2016-08-19T10:15:32.213 に答える