PHP 中的关联数组中的另一个键具有相同的值时,如何对不同键的值求和?
How to sums up values of a different key when same value is for another key in an associative array in 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)
);
这里我想计算他在每个特定位置花费的总时间,即如果 location
的键相同,则累加键 time_spent
的值。然后将其记录在一个新的关联数组(比如 $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)
);
<?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
)
)
你可以试试这个。
我创建了一个新数组 $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
)
)
我的回答向您展示了我如何获得您想要的数组,以及如果是我编码的话我会使用的实际数组。如果我想要的只是位置和花费的总时间的摘要,我会处理一个不需要继续存储标签的更紧凑的数组。不管怎样,如果你不想要摘要位,如果你认为这个版本更紧凑,就把它去掉:)
$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
)
)
请参考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)
);
这里我想计算他在每个特定位置花费的总时间,即如果 location
的键相同,则累加键 time_spent
的值。然后将其记录在一个新的关联数组(比如 $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)
);
<?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
)
)
你可以试试这个。
我创建了一个新数组 $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
)
)
我的回答向您展示了我如何获得您想要的数组,以及如果是我编码的话我会使用的实际数组。如果我想要的只是位置和花费的总时间的摘要,我会处理一个不需要继续存储标签的更紧凑的数组。不管怎样,如果你不想要摘要位,如果你认为这个版本更紧凑,就把它去掉:)
$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
)
)