2016-06-07 63 views
1

我有以下數組,其中我想總結所有條目的源和目標是相同的total_volume。PHP總結陣列條目,其中兩個鍵具有相同的值

Array (
[0] => Array 
    (
     [source] => ABC 
     [target] => DEF 
     [total_volume] => 10 
    ) 

[1] => Array 
    (
     [source] => ABC 
     [target] => GHI 
     [total_volume] => 5 
    ) 
[2] => Array 
    (
     [source] => ABC 
     [target] => DEF 
     [total_volume] => 5 
    ) 
) 

結果數組應該是這樣的:

ResultArray (
[0] => Array 
    (
     [source] => ABC 
     [target] => DEF 
     [total_volume] => 15 
    ) 

[1] => Array 
    (
     [source] => ABC 
     [target] => GHI 
     [total_volume] => 5 
    ) 
) 

我首先想到的是到LLOP通過現有的陣列,並通過在ResultArray嵌套循環檢查是否有匹配的源極的條目目標對已經存在。

是否有其他方式使用array_walk()或類似的方法?

在此先感謝您的幫助!

+0

我可以使用原始代碼,你需要與'array_walk'或原始的? – 2016-06-07 09:46:36

+0

我會很高興與任何解決方案,因爲我不知道如何解決這個問題。所以原始作品對我來說很好。 非常感謝提前! – mxzwrnz

+0

[獲取具有相同鍵值php數組的值的總和]的可能重複(http://stackoverflow.com/questions/37654630/get-sum-of-values-which-have-same-value-for-key -php-array) –

回答

2

不漂亮,但繼承人我如何與一個嵌套的foreach做到這一點;

$aStartingArray = array(); 
$aStartingArray[] = array('source'=>'ABC', 'target' => 'DEF', 'total_volume' => 10); 
$aStartingArray[] = array('source'=>'ABC', 'target' => 'GHI', 'total_volume' => 5); 
$aStartingArray[] = array('source'=>'ABC', 'target' => 'DEF', 'total_volume' => 5); 


$aSortedArray = array(); 

foreach ($aStartingArray as $aArray) { 

    $bSet = false; 

    foreach ($aSortedArray as $iPos => $aTempSortedArray) { 

     if(
      $aTempSortedArray['source'] == $aArray['source'] && 
      $aTempSortedArray['target'] == $aArray['target']){ 

      $aSortedArray[$iPos]['total_volume'] += $aArray['total_volume']; 

      $bSet = true; 
     } 

    } 

    if(!$bSet) { 

     $aSortedArray[] = array(
      'source' => $aArray['source'], 
      'target' => $aArray['target'], 
      'total_volume' => $aArray['total_volume'] 
      ); 
    } 
} 


var_dump($aSortedArray); 
1

這是一個快速的嘗試(對不起,它採用兩次array_walk)

<?php 

$a = [ 
    ['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 10 ], 
    ['source' => 'ABC', 'target' => 'GHI', 'total_volume' => 5 ], 
    ['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 5 ], 
]; 

$tmp = []; 

array_walk($a, function (&$item, $key) use (&$tmp) { 
    $resKey = $item['source'].'_'.$item['target']; 
    if (!isset($result[$resKey])) { 
     $result[$resKey] = 0; 
    } 
    $tmp[$resKey] += $item['total_volume']; 
}); 

$result = []; 
array_walk($tmp, function (&$item, $key) use (&$result) { 
    list ($source, $target) = explode('_', $key); 
    $result[] = ['source' => $source, 'target' => $target, 'total_volume' => $item]; 
}); 

var_dump($result); 
+0

我也試過了,但沒有完成,很好的回答。 –

1

這個怎麼樣?我覺得有點簡單。

$a = [ 
    ['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 10 ], 
    ['source' => 'ABC', 'target' => 'GHI', 'total_volume' => 5 ], 
    ['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 5 ], 
]; 
$result = $result2 = []; 
array_walk($a, function(&$item, $key) use(&$result) { 
    if(! isset($result[$item['source']])) 
     $result[$item['source']] = []; 
    if(! isset($result[$item['source']][$item['target']])) 
     $result[$item['source']][$item['target']] = 0; 
    $result[$item['source']][$item['target']] += $item['total_volume']; 
}); 
foreach($result as $key => $val) { 
    foreach($val as $k => $v) { 
     $result2[] = [$key,$k,$v]; 
    } 
} 
print_r($result2); 
0

另一種選擇。不是最好的,但會工作。

$arr = [ 
    ['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 10 ], 
    ['source' => 'ABC', 'target' => 'GHI', 'total_volume' => 5 ], 
    ['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 5 ], 
]; 

$res = $resulrArr = []; 
foreach($arr as $record){ 
    $record = array_values($record); 
    $res[$record[0].'_'.$record[1]] = !empty($res[$record[0].'_'.$record[1]]) ? $res[$record[0].'_'.$record[1]] + $record[2] : $record[2]; 
} 

$resulrArr = array_map(function($v,$k){ 
    $data = explode('_',$k); 
    $resulrArr['source'] = $data[0]; 
    $resulrArr['target'] = $data[1]; 
    $resulrArr['total_volume'] = $v; 
    return $resulrArr; 
},$res,array_keys($res)); 

echo '<pre>'; print_r($resulrArr);