【发布时间】:2013-02-08 11:16:11
【问题描述】:
这里是我想要完成的一些背景知识。我有一个来自正在显示的 MySQL 查询的数组。我想根据一个因素对数组进行排序。该因子是根据文章发布的时间和收到的投票数内联计算的。像这样的:
// ... 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是什么? “投票”在哪里发挥作用?我没有看到任何提及。 -
该因子未存储在 MySQL 中。它是根据时间和投票动态生成的。投票是 $rValue - 在问题中重命名,因此更清楚。 :) 投票基本上是从数组内的“news_points”值填充的。 $record 是我正在运行的每个语句的。
-
@viablepath:那么您如何计算
news_points?它是某种总数,还是仅是记录中列出的值? -
就是记录中的值。每次记录投票时,我都会更新记录。
-
我知道这个因子没有存储在 MySQL 中,但您当然可以将其作为查询的一部分进行计算,然后对其进行排序。