简体   繁体   English

什么是在PHP中排序这个数组的最佳方法?

[英]Whats the best way to sort this array in php?

I cant come up with any sane solution for this problem without resorting to ridiculous combinations of custom functions. 我不能为这个问题提出任何理智的解决方案,而不诉诸自定义函数的荒谬组合。 Maybe you can provide some fresh thought on this. 也许你可以提供一些新的想法。

I have the follow (simplified) array 我有跟随(简化)数组

Array
(
    [0] => Array
        (
            [vid_id] => 420037
            [vid_rating] => 2.93827
            [vid_quality] => 2
            [vid_special] => 1
            [vid_weight] => 0
            [vid_position] => 0
            [vid_position_end] => 0
        )

    [1] => Array
        (
            [vid_id] => 420040
            [vid_rating] => 3
            [vid_quality] => 1
            [vid_special] => 1
            [vid_weight] => 0
            [vid_position] => 0
            [vid_position_end] => 0
        )

    [2] => Array
        (
            [vid_id] => 426455
            [vid_rating] => 3.25581
            [vid_quality] => 2
            [vid_special] => 0
            [vid_weight] => 5
            [vid_position] => 1
            [vid_position_end] => 2
        )

    [3] => Array
        (
            [vid_id] => 429804
            [vid_rating] => 3
            [vid_quality] => 2
            [vid_special] => 0
            [vid_weight] => 0
            [vid_position] => 0
            [vid_position_end] => 0
        )

    [4] => Array
        (
            [vid_id] => 420848
            [vid_rating] => 2.94444
            [vid_quality] => 2
            [vid_special] => 0
            [vid_weight] => 3
            [vid_position] => 1
            [vid_position_end] => 2
        )

    [5] => Array
        (
            [vid_id] => 420859
            [vid_rating] => 2.73077
            [vid_quality] => 2
            [vid_special] => 0
            [vid_weight] => 4
            [vid_position] => 1
            [vid_position_end] => 2
        )

    [6] => Array
        (
            [vid_id] => 420524
            [vid_rating] => 2.41379
            [vid_quality] => 2
            [vid_special] => 0
            [vid_weight] => 5
            [vid_position] => 2
            [vid_position_end] => 2
        )

    [7] => Array
        (
            [vid_id] => 419810
            [vid_rating] => 3.13393
            [vid_quality] => 1
            [vid_special] => 0
            [vid_weight] => 0
            [vid_position] => 0
            [vid_position_end] => 0
        )

    [8] => Array
        (
            [vid_id] => 419851
            [vid_rating] => 2.97802
            [vid_quality] => 1
            [vid_special] => 0
            [vid_weight] => 5
            [vid_position] => 1
            [vid_position_end] => 2
        )

    [9] => Array
        (
            [vid_id] => 419843
            [vid_rating] => 2.95349
            [vid_quality] => 1
            [vid_special] => 0
            [vid_weight] => 3
            [vid_position] => 1
            [vid_position_end] => 2
        )

    [10] => Array
        (
            [vid_id] => 419838
            [vid_rating] => 2.73529
            [vid_quality] => 1
            [vid_special] => 0
            [vid_weight] => 4
            [vid_position] => 1
            [vid_position_end] => 2
        )

)

This array is a result of this mysql query 这个数组是这个mysql查询的结果

    SELECT 
    vid_id,
    vid_rating,
    vid_quality,
    vid_special,
    vid_weight,
    vid_position,
    vid_position_end
FROM versions
WHERE vid_movid = 'xxxxx' AND vid_status = 1 
ORDER BY vid_special DESC, vid_quality DESC, vid_rating DESC

This outputs a list of website links (actual link column is removed for simplicity), that need to be put in a very specific order. 这将输出一个网站链接列表(为简单起见,删除了实际链接列),需要按特定顺序排列。 Doing what I need done is possible with several UNIONed queries.... but i really dont want to resort to that, since the cost will be very high, so I figured manipulating the arrays would be easier. 使用几个UNIONed查询可以做我需要做的事情....但我真的不想诉诸于此,因为成本会非常高,所以我认为操纵数组会更容易。

I need to selectively extract several links from this array, and stick them on top of the array, in a certain order. 我需要从这个数组中有选择地提取几个链接,然后按照一定的顺序将它们粘贴在数组的顶部。

  • Links marked with vid_position, and vid_position_end denote a range that this group will occupy. 标有vid_position的链接和vid_position_end表示该组将占用的范围。 Meaning that if there are several links marked with those positions, only 2 will be pushed to the top, if the range is 1-2. 这意味着如果有多个标记有这些位置的链接,则只有2个将被推到顶部,如果范围是1-2。 if its 1-3, then top 3 positions will be occupied. 如果是1-3,那么前三名将被占用。

  • The weight denotes the ORDER in which links must be sorted. 权重表示必须对链接进行排序的ORDER。 So if there are 5 different links, with 5 different weights in a position range of 1-2, only the top 2 weights will be pushed to the top. 因此,如果有5个不同的链接,在位置范围1-2中有5个不同的权重,则只有前2个权重将被推到顶部。 The remaining 3 will remain where they are. 剩下的3个将保持原样。

  • there are different groups of links. 有不同的链接组。 In this case, there is a group 1-2, and 3-3. 在这种情况下,有1-2组和3-3组。 First group occupies the first 2 positions, and in my example has 3 weight classes. 第一组占据前2个位置,在我的例子中有3个重量级别。 the other group occupires the 3rd position, and only has 1 weight class. 另一组占据第3位,只有1个重量级。

  • The ordering should be independent. 订购应该是独立的。 If there are no links in the 1-2 group, but there are links in 3-3, that means 3-3 grouped links will appear in the first position. 如果1-2组中没有链接,但3-3中有链接,则表示3-3个分组链接将出现在第一个位置。

For each element in the array, create an entry in a 'sort_by' array where the entry is set to a sort "score" that you create. 对于数组中的每个元素,在“sort_by”数组中创建一个条目,其中条目设置为您创建的排序“分数”。

The score is generated by your algorithm for ordering your entries. 分数由您的算法生成,用于订购您的条目。 Remember that you can create a score with large numbers. 请记住,您可以创建一个包含大数字的分数。 Eg, the most minor sort order is multiplied by 10, the next most minor sort order is multiplied by 100, next by 1000, then add up the numbers to get the score for the entry. 例如,最小的排序顺序乘以10,下一个最小的排序顺序乘以100,然后乘以1000,然后将数字相加以获得条目的分数。

Then use Sort an Array by keys based on another Array? 然后使用基于另一个数组的键排序数组? to sort the main array by the sort_by array 通过sort_by数组对主数组进行排序

Added: This method is good if it is slow or expensive to figure out ordering between two original array elements since the scoring is only done once per original array element. 补充:如果计算两个原始数组元素之间的排序很慢或很昂贵,这种方法很好,因为每个原始数组元素只进行一次评分。 If the ordering is easy, then using uasort will probably be clearer code. 如果顺序很简单,那么使用uasort可能会更清晰。

对于手工定制的数组排序,我会使用uasort或其中一个相关函数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM