2013-07-29 77 views
0

我已经帮助创建了一个从关联数组中获取元素的组合数组,这些数组具有从搜索引擎中获取的url和分数,当urls匹配时,分数被添加在一起组合阵列,这里是代码在组合数组中添加额外的元素

$combined = array(); 

foreach($bingArray 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 
} 

则相同的代码跑了$ googleArray,这工作得很好,但现在我想补充一点,有以下

foreach($jsonObj->d->results as $value) 
    { $i = 0; 
     $bingArray[str_replace ($find, '', ($value->{'Url'}))] = array(   
     //'title'=> $value->{'Title'}, 
     //'snippet' => $value->{'Description'}, 
     'score' => $score-- 
    ); 
代码被注释掉的值

我敢肯定,它很容易改变第一个foreach循环做到这一点,但我不知道如何,请任何帮助请

+0

可不可以给结果的例子分数? –

回答

0

答案是将每个数组元素定义为包含分数,标题和snippet元素的关联数组。

$combined = array(); 

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

要访问你只是

echo $combined[$key]["score"]; 
+0

我似乎在代码 – user2622398

+0

的最后一行出现语法错误,他忘记在分号前添加')'。 – Eray

+0

opps ...我在代码片段中也有a =而不是=>。更新的代码。 – Orangepill