Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于数组值的速率数组_Php_Arrays_Sorting - Fatal编程技术网

Php 基于数组值的速率数组

Php 基于数组值的速率数组,php,arrays,sorting,Php,Arrays,Sorting,我需要根据它们的发生率对它们进行评级,如下所示 occurrence of A----5 times occurrence of B----7 times occurrence of C----6 times ($total['A']=5; $total['B']=7; $total['C']=6;) 您还可以尝试使用数组函数,如先对值进行排序,然后获取排序数组的键,然后使用提取的键作为值创建另一个数组。现在,它们的排名得到了相同的数组,不同的是,当数组以索引0开始时,排名第一的是“0”

我需要根据它们的发生率对它们进行评级,如下所示

occurrence of A----5 times
occurrence of B----7 times
occurrence of C----6 times
($total['A']=5;
 $total['B']=7;
 $total['C']=6;)

您还可以尝试使用数组函数,如先对值进行排序,然后获取排序数组的键,然后使用提取的键作为值创建另一个数组。现在,它们的排名得到了相同的数组,不同的是,当数组以索引0开始时,排名第一的是“0”

可能您可以使用“0”填充一个附加值,然后最终将其删除

像这样的,

asort(&$total);
$rating = 1;
foreach (array_reverse($total) as $key => $val)
    $total[$key] = $rating++;
或者,如果您不喜欢填充额外的值,可以这样尝试。使用键和排序数组的计数创建一个数组

$total['A']=5;
$total['B']=7;
$total['C']=6;

$total[0]=0;
asort($total);
$total = array_slice(array_flip(array_keys($total)),1);

这可能是一种快速简便的方法。希望这能有所帮助。

当两个总数相等时会发生什么?@OneTrickPony代码现在格式正确了。:-)
$total['A']=5;
$total['B']=7;
$total['C']=6;

$total[0]=0;
asort($total);
$total = array_slice(array_flip(array_keys($total)),1);
asort($total);
$total = array_combine(array_keys($total),range(1, count($total)));