按数字键对数组进行不自然的排序
我试图按数组的数字键对数组进行排序,就好像它们不是数字一样——我不想要自然排序。
$arr = [
'1000' => 'DUMMY',
'1001' => 'TEST',
'100001' => 'DUMMY1',
'100002' => 'DUMMY3',
'100004' => 'DUMMY4',
'100100' => 'test1',
'100102' => 'DUMMY123'
];
排序后,结果应该是:
[
'1000' => 'DUMMY',
'100001' => 'DUMMY1',
'100002' => 'DUMMY3',
'100004' => 'DUMMY4',
'1001' => 'TEST',
'100100' => 'test1',
'100102' => 'DUMMY123'
]
I'm trying to sort an array by its numeric keys as if they were not numbers -- I don't want natural sorting.
$arr = [
'1000' => 'DUMMY',
'1001' => 'TEST',
'100001' => 'DUMMY1',
'100002' => 'DUMMY3',
'100004' => 'DUMMY4',
'100100' => 'test1',
'100102' => 'DUMMY123'
];
After sorting, the result should be:
[
'1000' => 'DUMMY',
'100001' => 'DUMMY1',
'100002' => 'DUMMY3',
'100004' => 'DUMMY4',
'1001' => 'TEST',
'100100' => 'test1',
'100102' => 'DUMMY123'
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为您的数组键是“big-endian”,所以您可以将键显式排序为字符串(覆盖 `sort() 的默认行为以按数字方式对数值进行排序)。 (演示)
Because your array keys are "big-endian", you can explicitly sort the keys as strings (overriding the default behavior of `sort() to sort numeric values numerically). (Demo)
我不太确定明白你想要什么。
但我猜是这样的:
对数组进行排序
输出:
I'm not really sure understand what you want.
But i guess it's something like that:
this sort the array
Output:
无论您添加多少个额外的 2 字符子类别,asort() 都应该能够解决问题。使用 SORT_STRING 标志,类别甚至不必是字符串。
应该导致
asort() should do the trick no matter how many extra 2-character subcategories you add. With the SORT_STRING flag the category doesn't even have to be a string.
Should result in