当找到特定值时,按列值对多维数组的行进行排序,并使用粘性第一行
我的多维数组按 OwnerNickName
字段按字母顺序排序,但我需要将 OwnerNickName = 'My House'
行作为粘性第一行,然后按 OwnerNickname 排序其他所有内容上升。
输入:
[
'0318B69D-5DEB-11DF-9D7E-0026B9481364' => [
'OwnerNickName' => 'andy',
'Rooms' => [ /* irrelevant */ ]
],
'286C29DE-A9BE-102D-9C16-00163EEDFCFC' => [
'OwnerNickName' => 'anton',
'Rooms' => [ /* irrelevant */ ]
],
'8BE18F84-AC22-102D-9C16-00163EEDFCFC' => [
'OwnerNickName' => 'mike',
'Rooms' => [ /* irrelevant */ ]
],
'29B455DE-A9BC-102D-9C16-00163EEDFCFC' => [
'OwnerNickName' => 'My House',
'Rooms' => [ /* irrelevant */ ]
]
]
My multidimensional array is sorted alphabetically by the OwnerNickName
field, but I need to make the OwnerNickName = 'My House'
row as the sticky first row and then everything else sorted by OwnerNickname ascending.
Input:
[
'0318B69D-5DEB-11DF-9D7E-0026B9481364' => [
'OwnerNickName' => 'andy',
'Rooms' => [ /* irrelevant */ ]
],
'286C29DE-A9BE-102D-9C16-00163EEDFCFC' => [
'OwnerNickName' => 'anton',
'Rooms' => [ /* irrelevant */ ]
],
'8BE18F84-AC22-102D-9C16-00163EEDFCFC' => [
'OwnerNickName' => 'mike',
'Rooms' => [ /* irrelevant */ ]
],
'29B455DE-A9BC-102D-9C16-00163EEDFCFC' => [
'OwnerNickName' => 'My House',
'Rooms' => [ /* irrelevant */ ]
]
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想实现自己的排序功能,例如:
You may want to implement your own sorting function, e.g.:
如果您想改变对哪个索引进行排序或哪个值应该特殊的想法,类似这样的内容可能适合:
给出结果:
If you want to change your mind about which index to sort on or which value should be special, something like this might suit:
Gives the result:
要保留原始的第一级键,
uasort()
。将排序规则作为二元素数组传递给回调函数,以便由 spaceship 运算符进行比较。由于false
“小于”true
,因此请检查 OwnerNickName 是否不等于 My House,以使其成为粘性第一个元素。代码:(演示)
To preserve the original first level keys,
uasort()
. Pass the sorting rules to the callback as two-element arrays to be compared by the spaceship operator. Becausefalse
is "less than"true
, check if the OwnerNickName is NOT equal to My House to make it the sticky first element.Code: (Demo)