舍入和乘法后返回array_count_values时出错

时间:2010-11-23 18:35:47

标签: php

我希望能够以下列方式使用array_count_values:

$roundedans = 10 * round($groupdiff, 1);
$diffarray[] = $roundedans;

print_r(array_count_values($diffarray));

前两行是while循环的一部分,它将$ roundans插入到数组中,array_count_values在循环外部调用。

当我尝试运行代码时出现此错误:

Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in...

即使我通过乘以10将所有值转换为整数。

任何帮助都将不胜感激。

这是print_r的输出:

Array ( [0] => 6 [1] => 15 [2] => 8 [3] => 13 [4] => -60 [5] => 1 [6] => -61 [7] => 7 [8] => 49 [9] => 26 [10] => -3 [11] => -66 [12] => 20 [13] => 10 [14] => 6 [15] => -94 [16] => -1 [17] => -6 [18] => -19 [19] => -1 [20] => 48 [21] => -4 [22] => 45 [23] => 21 [24] => -11 [25] => 19 [26] => 1 [27] => -10 [28] => 4 [29] => -14 [30] => 26 [31] => -1 [32] => -20 [33] => 8 [34] => -17 [35] => -2 [36] => -6)

没有非整数。

1 个答案:

答案 0 :(得分:1)

使用:

$diffarray[] = intval($roundedans);

由于round()始终返回float,如果将其乘以10,则类型不会改变。

intval()将改变类型。