这里是我想要完成的一点小背景。我有一个正在显示的MySQL查询中的数组。我想根据一个因素对数组进行排序。系数根据文章发布时间以内嵌方式计算得出&它收到的投票数。事情是这样的:PHP - 基于排序数组的排序因子:: usort?
// ... MySQL query here
$votes = $row['0']
$seconds = strtotime($record->news_time)+time();
$sum_total = pow($votes,2)/$seconds;
所以阵列多数民众赞成在未来看起来是这样的:
Array (
[0] => stdClass Object (
[id] => 13
[news_title] => Article
[news_url] => http://website.com/article/14
[news_root_domain] => website.com
[news_category] => Business
[news_submitter] => 2
[news_time] => 2013-02-18 12:50:02
[news_points] => 2
)
[1] => stdClass Object (
[id] => 14
[news_title] => Title
[news_url] => http://www.website.com/article/2
[news_root_domain] => www.website.com
[news_category] => Technology
[news_submitter] => 1
[news_time] => 2012-10-02 10:03:22
[news_points] => 8
)
)
我想用我上面提到的因素上述的阵列进行排序。这个想法是显示列表中最高评分的文章(使用计算的因子),而不是数组进入的默认排序方法。看起来像usort可能是我最好的选择,但让我知道你都觉得呢?
为什么不使用MySQL对这个因素进行排序?否则,'$ record'是什么? “投票”在哪里起作用?我没有看到这一点。 – Madbreaks 2013-02-22 18:38:13
该因子未存储在MySQL中。它是基于时间和投票动态生成的。票数是$ rValue - 在问题中重新命名,所以更清楚。 :)投票基本上来自数组内的“news_points”值。 $ record是我正在运行的每条语句。 – viablepath 2013-02-22 18:44:00
@viablepath:那么你如何计算'news_points'?这是一种总计,还是只是记录中列出的价值? – prodigitalson 2013-02-22 18:55:03