2016-08-19 35 views
0

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

在这里我要计算他在每一个具体的位置花费的总时间,即积累的关键time_spent值如果location关键是一样的。然后将其记录在一个新的关联数组中(如$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) 
); 
+2

请更新您的问题,包括您有任何企图(和这些尝试的错误),以达到预期的效果 – Epodax

+0

@Epodax,当然,我会确保在下一个问题中做到这一点。对不起,我没有以前的尝试,因为我已经替换了给定的答案。无论如何感谢增量反馈。 – Wenuka

回答

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 
     ) 

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

    ) 
相关问题