组合数组并添加分数

ben_eire

我必须关联从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不匹配然后我只想将其插入数组。

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

Yotam单体

请尝试以下方法:

$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

您可以使用函数来代替代码。.如果您要分析更多的搜索引擎,则将是最佳选择。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章