简体   繁体   English

按值对数组排序,然后在这些值内随机分组,但将某些元素分组-还是修改MySQL查询以将所有元素组合在一起?

[英]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匹配的所有条目?

You could shuffle() your array results. 您可以shuffle()您的数组结果。 Example from PHP.net: 来自PHP.net的示例:

<?php
   $numbers = range(1, 20);
   shuffle($numbers);
   foreach ($numbers as $number) {
      echo "$number ";
   }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP Mysql查询获取特定日期范围内所有星期一的时间范围,并将这些不同的开始和结束时间放入数据库中 - PHP Mysql query that gets a timeframe of all mondays within a certain date range and puts those different start and end times in a database PHP按值分组并随机排序 - PHP Group by value and sort randomly for循环内的mysql查询未插入所有值 - Mysql query within for loop not inserting all values 按字母顺序对数组进行排序,但将具有 0 值的数组移到末尾 - Sort an Array alphabeticaly but move those with 0 values to the end 高级MySQL查询(分组依据和排序) - Advanced MySQL Query (Group By and Sort) PHP / MySQL-创建不同值的数组,查询数据库表以获取与这些值相关的数据,并为每个值循环 - PHP / MySQL - Create array of distinct values, query db table for data associated with those values, and loop for each PHP-重新排列数组(如果它包含某些值)并且这些数组按特定顺序排列 - PHP - reorder array if it contains certain values and those arrays are in a certain order MySQL将某些结果组合成一个数组 - MySQL group certain results into an array MySQL SQL对字段中的某些单词进行排序 - MySQL SQL Sort on certain words within a field php 数组中的数组并调用这些数组中的某些值 - php Arrays within arrays and calling certain values inside those arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM