0

请参考 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如果 keylocation相同,则累积 key 的值。然后将其记录在一个新的关联数组(比如$place_array)中,其中的值与键locationtotal_time.

所以输出数组应该是

$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
<?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>';
?>

输出

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:01:46.000 回答
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,在这个数组中,我保存了位置为的数组,AA下次我检查位置是否为AA它所花费的时间,然后将其花费的时间添加到上一个,如果不是,则添加新的. 最后,我曾经array_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 回答