在2D关联数组中使用multi_sort创建排序列表


Using multi_sort in a 2D associated array to create a ranked list

下面的代码组合了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 ) 

任何帮助都将是伟大的家伙和玩偶

[这应该是一条评论,但由于我的状态(声誉<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);

我把这个代码放进去,它在中工作

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

感谢您的帮助