2012-07-16 56 views
0

当重复在URL找到我想:找到阵副本,添加到原来的,然后删除

  1. 采取“分数”,并把它添加到原
  2. 采取“引擎”字符串其追加到原来的
  3. 然后删除整个重复条目
array 
    0 => 
    array 
     'url' => string 'http://blahhotel.com/' 
     'score' => int 1 
     'engine' => string 'cheese' 
    1 => 
    array 
     'url' => string 'http://www.blahdvd.com/' 
     'score' => int 2 
     'engine' => string 'cheese' 
    2 => 
    array 
     'url' => string 'http://blahhotel.com/' 
     'score' => int 1 
     'engine' => string 'pie' 
    3 => 
    array 
     'url' => string 'http://dictionary.reference.com/browse/blah' 
     'score' => int 2 
     'engine' => string 'pie' 
    4 => 
    array 
     'url' => string 'http://dictionary.reference.com/browse/blah' 
     'score' => int 1 
     'engine' => string 'apples' 

它应该是这样的结尾:

array 
    0 => 
    array 
     'url' => string 'http://blahhotel.com/' 
     'score' => int 2 
     'engine' => string 'cheese, pie' 
    1 => 
    array 
     'url' => string 'http://www.blahdvd.com/' 
     'score' => int 2 
     'engine' => string 'cheese' 
    3 => 
    array 
     'url' => string 'http://dictionary.reference.com/browse/blah' 
     'score' => int 3 
     'engine' => string 'pie, apples' 
+0

这是太辛苦了。 – hjpotter92 2012-07-16 17:48:39

+0

试图什么也没做,失败了? – alfasin 2012-07-16 17:53:03

+0

我一直在工作几个小时。我将包含我的代码片段,但它可能没有帮助。我会随着我的进展保持最新状态。没有必要是sn。。谢谢。 – flux 2012-07-16 17:55:40

回答

0

我相信这符合您的要求。

基于您提供的期望输出,您似乎希望保留每个条目的数字索引。如果您实际上不需要保留这些数字,则可以删除第二个foreach循环和有关$indices变量的行,然后仅返回$tmpList

function reduceEntries($entries) 
{ 
    $tmpList = array(); 
    $indices = array(); 

    foreach ($entries as $i => $entry) { 
     if (isset($tmpList[$entry['url']])) { 
      $tmpList[$entry['url']]['score'] += $entry['score']; 
      $tmpList[$entry['url']]['engine'] .= ', ' . $entry['engine']; 
     } else { 
      $tmpList[$entry['url']] = $entry; 
      $indices[$entry['url']] = $i; 
     } 
    } 

    // rebuild final array with indices 
    $finalList = array(); 
    foreach ($tmpList as $url => $entry) { 
     $finalList[$indices[$url]] = $entry; 
    } 

    return $finalList; 
} 

(这里的a working example上键盘。)

相关问题