基于评分公式的 PHP 数组排序?

标签 php mysql arrays

我正在尝试使用我创建的评级论坛对结果数组进行排序,它的工作原理是根据发布后的时间创建一个新的评级值。

评分公式如下:评分随时间减少(时间采用 unix 时间戳格式)

$new_rating = $current_rating / ($current_time - $post_time);

Array
(
[0] => Submissions Object
    (
        [title] => Exploits Emerge For Linux Privilege Escalation Flaw
        [link] => http://test.com
        [description] => test description test
        [tags] => hardware,government,libya
        [rating] => 10
        [date] => 1327546314
    )

[1] => Submissions Object
    (
        [title] => High School Students Send Lego Man 24 Kilometers High
        [link] => http://test.com
        [description] => test description test
        [tags] => hardware,government,libya
        [rating] => 5
        [date] => 1327546305
    )

)

我的问题是如何使用上面的公式对这个数组进行排序?或者他们是我可以将此公式插入 mysql 查询语句的方法吗?

非常感谢大家。

最佳答案

您将要使用 usort以及自定义排序功能。

function get_rating($obj)
{
    $obj->rating / (time() - $obj->date);
}

function my_compare_func($a, $b)
{
    $a = get_rating($a);
    $b = get_rating($b);
    if($a == $b) return 0;
    return ($a > $b)? 1: -1;
}

usort($array_of_objs, 'my_compare_func');

关于基于评分公式的 PHP 数组排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9013678/

相关文章:

php - 将多个表单输入组合成键对值数组并为值添加维度

ruby-on-rails - 使用 ruby​​ 格式化数据数组

java - 如何让我的 Java 代码在连接后显示结果?

php - 在已经运行的查询循环中从第二个表调用循环查询

php require_once "FILENAME"导致需要打开失败 ''

mysql - 导入错误:没有名为 connector.conversion 的模块

javascript - .map() 未定义,即使我传递了一个数组

javascript - 在 Laravel 5.3 中动态更改图像

sql - 存储过程中的 LEFT JOIN 问题

mysql - where子句中条件的优先级是什么?