0

我在创建一个从关联数组中获取元素的组合数组方面得到了帮助,这些数组具有从搜索引擎获取的 url 和分数,当 url 匹配时,分数被添加到组合数组中,这里是代码

$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 循环来做到这一点很简单,但我不知道怎么做,请帮忙

4

1 回答 1

0

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

$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"];
于 2013-07-29T14:32:55.410 回答