2016-08-19 34 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 
      ) 

    ) 
相關問題