查找重复项,添加其整数值并删除重复的PHP

时间:2013-07-26 11:46:35

标签: php integer duplicates

确定。我目前正在尝试为大学项目(Metasearch引擎)开发聚合技术。我有三个搜索引擎有三个阵列。每个数组都是这样的:

// Array created to store the query results
$GoogleArray = array ();

//Top score for Rank 1
$score=100;
//Prefix/specifier to be replaced by space for comparing the URL
$prefix = array ('http://','https://','www.');

//The variables will now be populated with values from the json(results)
foreach($js->items as $item){
    $link2 =  str_replace ($prefix, '', $item->link );
    $link = $item->link;
    $title = $item->title;
    $snippet = $item->snippet;
    //Decrements through score for each result
    $scores = $score--;
    $GoogleArray[] = array($link, $title, $snippet, $scores, $link2);
}

我已将三个数组合并为一个大型列表(这很棒):

//Find duplicates:Step3:Merge Arrays (one long list)
$MergedArr=array_merge($BingArray, $GoogleArray, $BlekkoArray);

然后我需要找到重复项,将它们的分数加在一起(因此最高分会到达聚合列表的顶部;稍后排序数组)并删除重复的条目:

//Find duplicates:Step4:Find the duplicates and add them together (re-score)
//////////////////////////////////////////////
//http://stackoverflow.com/questions/5821948/
/////////////////////////////////////////////

$newArr = array();

//sum the values in a specific key of that array
foreach($MergedArr as $row){
    if($link2===$link2){
        $key = substr($row[4],0);

        $newArr[$key][0]=$row[0];
        $newArr[$key][1]=$row[1];
        $newArr[$key][2]=$row[2];
        $newArr[$key][3]+=$row[3]; //adding the values (int) here ("Scores")
    }else{
        echo "There are no duplicates";
    }
}

第4步做了那些事,但现在它就像$ newArr [“修剪过的网址”] [3];我需要按降序排序它们[array_multisort()]但不完全确定它是否会因$ key更改而正常工作。

他们是更好的方式来做第4步还是我仍然可以先进行多项?

提前致谢。

修改

array(30) { 

[0]=> array(5) 

{ 
[0]=> string(35) "http://www.speakeasy.net/speedtest/" 
[1]=> string(20) "Speakeasy Speed Test" 
[2]=> string(109) "Test your Internet Connection with Speakeasy's reliable and accurate broadband speed test. What's your speed?" 
[3]=> int(100) 
[4]=> string(24) "speakeasy.net/speedtest/" 
} 

[1]=> array(5) 

{ 
[0]=> string(20) "http://www.test.com/" 
[1]=> string(12) "Test Central" 
[2]=> string(158) "Test.com provides a complete software solution for creating online tests and managing enterprise and specialist certification programs, in up to 22 languages." 
[3]=> int(99) 
[4]=> string(9) "test.com/" 
} 

[2]=> array(5)

{ 
[0]=> string(33) "http://en.wikipedia.org/wiki/Test" 
[1]=> string(39) "Test - Wikipedia, the free encyclopedia" 
[2]=> string(165) "Test, TEST or Tester may refer to: Contents 1 Assessment 2 Science and technology 3 People 4 Media 5 Other 6 See also Assessment Test (assessment), an assessment ..." 
[3]=> int(98) 
[4]=> string(26) "en.wikipedia.org/wiki/Test" 
} 

var_dump($ MergedArr)之后的一些数组的示例;

0 个答案:

没有答案