gpt4 book ai didi

php - 拉维尔 5 : Is there a non-case sensitive way to sort a collection by an attribute?

转载 作者:可可西里 更新时间:2023-11-01 13:37:33 24 4
gpt4 key购买 nike

我正在努力使用 sortBy() 方法对 Eloquent 集合进行排序。问题是排序区分大小写,它首先检索大写结果,然后检索小写结果,但我想要实现的是对每个项目进行排序,无论它是大写还是小写。

最佳答案

sortBy() 第二个参数允许您设置一些关于应该如何处理排序的标志。

标志与 PHP sort() 原生函数完全相同。

  • SORT_REGULAR - compare items normally (don't change types)
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
  • SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
  • SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

source: php.net

您可以尝试使用 $collection->sortBy('key', SORT_NATURAL|SORT_FLAG_CASE)

关于php - 拉维尔 5 : Is there a non-case sensitive way to sort a collection by an attribute?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42925120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com