[英]Sort array by value, then randomly within those values BUT group certain elements - or revise MySQL query to combine it all?
So, let's say I want to sort an array in this order: 1) sort by total_weight 2) sort within those weights randomly 3) if there's an item within the same weight that is similar in name, group those items 因此,假设我要按以下顺序对数组进行排序:1)按total_weight排序2)在这些权重内随机排序3)如果在相同权重内有一个名称相似的项目,请将这些项目分组
This would result in something like so: 这将导致如下所示:
name: Delta 1 - total_weight: 5
name: Delta 1b - total_weight: 5
name: Charlie 12 - total_weight: 5
name: Charlie 12b - total_weight: 5
name: Charlie 12c - total_weight: 5
name: Alpha 10 - total_weight: 4
name: Delta 2 - total_weight: 3
name: Delta 2b - total_weight: 3
name: Bravo 5 - total_weight: 3
The array prior to sorting looks like so: 排序之前的数组如下所示:
Array
(
[463] => stdClass Object
(
[name] => Delta 1b
[total_weight] => 5
)
[463] => stdClass Object
(
[name] => Charlie 12
[total_weight] => 5
)
[340] => stdClass Object
(
[name] => Charlie 12b
[total_weight] => 5
)
[340] => stdClass Object
(
[name] => Charlie 12c
[total_weight] => 5
)
[342] => stdClass Object
(
[name] => Delta 1
[total_weight] => 5
)
[532] => stdClass Object
(
[name] => Alpha 10
[total_weight] => 4
)
[203] => stdClass Object
(
[name] => Bravo 5
[total_weight] => 3
)
[206] => stdClass Object
(
[name] => Delta 2
[total_weight] => 3
)
[208] => stdClass Object
(
[name] => Delta 2b
[total_weight] => 3
)
I know that to sort by weight, then by alphabetical I could use usort: 我知道按重量排序,然后按字母顺序可以使用usort:
function _sort_by_total_weight($a, $b)
{
if ($a->total_weight == $b->total_weight) {
return strcmp($a->name, $b->name);
}
return ($a->total_weight > $b->total_weight) ? -1 : 1;
}
But, I want the results to not be alphabetical and feel random, yet group together some relevant tracks. 但是,我希望结果不要按字母顺序排列并且感觉随意,而要将一些相关的音轨归为一组。 I realize I could throw a row in the db for sort order or something, but with 10,000+ records, it's not easily implemented.
我意识到我可以将数据库中的行按排序顺序或其他方式进行排序,但是有10,000多个记录,这并不容易实现。 Any thoughts on how to crack this nut?
关于如何破解这个螺母有什么想法吗?
EDIT : So, I reworked this on the db side (MySQL) so that the query is weighted and ordered before it hits php: 编辑 :因此,我在数据库方面(MySQL)对此进行了重新设计,以便在查询到达php之前对其进行加权和排序:
$this->db->select('tracks.id,
tracks.name AS name,
tracks.filename,
tracks.url_name,
tracks.file_path_high,
tracks.filesize,
tracks.categories,
tracks.duration,
tracks.folder,
links.tag_id,
SUM(links.weight) AS total_weight,
tag_data.tag_name');
$this->db->distinct();
$this->db->from('music AS tracks');
$this->db->where_in('links.tag_id', array('1', '2');
$this->db->join('linked_tags AS links', 'links.track_id = tracks.id', 'inner');
$this->db->join('tags AS tag_data', 'tag_data.id = links.tag_id', 'inner');
$this->db->group_by("name", "total_weight");
$this->db->order_by('total_weight DESC, RAND(123)');
$this->db->limit($total, $offset);
But, now I have some near-duplicate entries in the result. 但是,现在结果中有一些重复的条目。 For the where_in, is there a way to state that I only want entries that have entries for both 1, and 2 and not have it return all entries where 1 matches OR 2 matches?
对于where_in,是否有一种方法可以声明我只希望具有同时包含1和2的条目的条目,而不返回包含1匹配或2匹配的所有条目?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.