0

以下代码组合了 3 个用 url 和分数填充的数组,如果 url 匹配,则将分数相加以创建一个新数组。我现在正在尝试按降序在组合数组中创建一个排名列表,但我不太确定该怎么做

<?php




$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
}

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

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


array_multisort($value['score'], SORT_DESC,$combined);

print_r($combined); // print results  
?>

以下是我目前得到的输出

Warning: array_multisort() [function.array-multisort]: 
Argument #1 is an unknown sort flag in   /homepublic_html/agg_results.php on line   230
Array ( [time.com/time/] => 200 [time.gov/] => 297 [timeanddate.com/worldclock/] 
=> 294 [timeanddate.com/] => 194  [en.wikipedia.org/wiki/Time] => 289 
[worldtimezone.com/] => 190 [time100.time.com/]=> 188 
[time.gov/timezone.cgi?  Eastern/d/-5/java] 
=> 186    [en.wikipedia.org/wiki/Time_(magazine)] 
=> 275   [dictionary.reference.com/browse/time] => 182 [time.com/] 
=> 100 [time.com/time/magazine]
=> 96 [time.is/] => 95 [tycho.usno.navy.mil/cgi-bin/timer.pl] 
=> 94 [twitter.com/TIME]   => 93 [worldtimeserver.com/] => 92 ) 

任何帮助都会是很棒的家伙和洋娃娃

4

2 回答 2

1

[这应该是评论,但由于我的身份(声誉<50),我只能写帖子......]

我再次查看了php 手册,发现对于您尝试执行的函数的第一个参数的排序任务,它需要是一个与您想要排序array_multisort的实际数组 ( ) 具有相同键的数组。$combined该参数$value不是一个数组,而只是一个存在于先前foreach循环范围内的变量。

您应该将代码修改为:

$score=array();    
foreach($bingArray as $key=>$value){ // for each bing item
    if(!isset($score[$key])) { $score[$key]=0; }
    $score[$key] += $value['score'] // add the score to $score
    $combined[$key] = $value;       // place the whole associative array into $combined
    }

然后稍后:

array_multisort($score, SORT_DESC, $combine);
于 2013-07-28T10:16:10.720 回答
0

我把这段代码放进去,它工作了

array_multisort($combined);
$reverse = array_reverse($combined, true);
print_r($reverse);

谢谢你的帮助

于 2013-07-29T09:58:22.857 回答