我无法为这个问题提出任何理智的解决方案,而不会诉诸于自定义函数的荒谬组合。也许你可以对此提出一些新的想法。
我有跟随(简化)数组
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
)
)
此数组是此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
这会输出一个网站链接列表(为简单起见,删除了实际链接列),需要按特定顺序排列。使用几个UNIONed查询可以做我需要做的事情....但我真的不想诉诸于此,因为成本会非常高,所以我认为操纵数组会更容易。
我需要从这个数组中有选择地提取几个链接,然后按照一定的顺序将它们粘贴在数组的顶部。
标有vid_position的链接,和 vid_position_end表示一个范围 这个群体将占据。意思是 如果标记了多个链接 这些职位只有2个 如果范围是,则推到顶部 1-2。如果它是1-3,那么前3 职位将被占用。
重量表示ORDER 链接必须排序。所以,如果有的话 5个不同的链接,有5个不同 重量在1-2的位置, 只会推动前2个重量 到顶部。剩下的3个会 保持原状。
有不同的链接组。 在这种情况下,有一组1-2, 和3-3。第一组占据了 前2个职位,在我的例子中 有3个重量级别。另一组 仅占第3位 有1个重量级。
订购应该是独立的。 如果1-2中没有链接 组,但3-3中有链接, 这意味着3-3组链接将 出现在第一位。
答案 0 :(得分:1)
对于数组中的每个元素,在'sort_by'数组中创建一个条目,其中条目设置为您创建的排序“得分”。
分数由您的算法生成,用于订购您的参赛作品。请记住,您可以创建一个包含大数字的分数。例如,最小的排序顺序乘以10,下一个最小的排序顺序乘以100,然后乘以1000,然后将数字加起来得到条目的分数。
然后使用Sort an Array by keys based on another Array? 通过sort_by数组对主数组进行排序
补充:如果计算两个原始数组元素之间的排序很慢或很昂贵,这个方法很好,因为每个原始数组元素只进行一次评分。如果顺序很简单,那么使用uasort可能会更清晰。
答案 1 :(得分:0)
对于手工定制的数组排序,我会使用uasort或其中一个相关函数。