0

我必须关联从 2 个搜索引擎生成的数组,这些搜索引擎返回以下内容

必应:

Array ( [cars.com/] => Array ( [score] => 100 )
        [car.com/] => Array ( [score] => 99 )
        [car.org/] => Array ( [score] => 98 ).....

谷歌:

Array ( [hertz.com/] => Array ( [score] => 100 )
        [edmunds.com/] => Array ( [score] => 99 )
        [thrifty.com/] => Array ( [score] => 98 )....

我想创建一个排名组合数组,bing 数组是排名,然后我想添加 Google 数组,如果 google url 数组匹配 Bing 数组中的 url 我想添加分数,如果 url 不匹配然后我只想将它插入到数组中。

任何想法我将如何实现这一点。问候

4

2 回答 2

1

Try the following:

$combined = array(); 

foreach($bing as $key=>$value){ // for each bing item
    if(isset($combined[$key]))
        $combined[$key] += $value['score']; // add the score if already exists in $combined
    else
        $combined[$key] = $value['score']; // set initial score if new
}

// do the same for google
foreach($google as $key=>$value){
    if(isset($combined[$key]))
        $combined[$key] += $value['score'];
    else
        $combined[$key] = $value['score'];
}

print_r($combined); // print results

You can use a function instead of duplicating the code.. will be best if you have more search engines to analyze.

于 2013-07-21T10:50:46.433 回答
0

实际上,对创建组合数组的性能与就地操作数组进行基准测试会很有趣。

foreach ($google as $k=>$v){
    if(isset($google[$k]) $bing[$k]['score'] += $v['score'];
        else $bing[$k] = $v;
于 2013-07-21T11:00:18.727 回答