按字母数字键自然对数组进行排序
如何按其键对这样的数组进行排序,从较小的分辨率到较大的分辨率:
$sizes = array(
'120x120' => 'large',
'60x60' => 'small',
'200x200' => 'very large',
'90x90' => 'medium',
...
?
应该是:
- 60x60
- 90x90
- 120x120
- 200x200
- ...
How can I sort a array like this by its keys, from the smaller resolution to the larger one:
$sizes = array(
'120x120' => 'large',
'60x60' => 'small',
'200x200' => 'very large',
'90x90' => 'medium',
...
?
should be:
- 60x60
- 90x90
- 120x120
- 200x200
- ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ksort()
< /a> 在数字模式应该工作得很好:会让你:
This will work because the size is a numeric -- and is found before the `'x'` *(not sure what will be done with the `'x'` and what follows -- but, anyway, that part of the keys is useless, as it's purely redondant information)*
ksort()
in numeric mode should work just fine :will get you :
This will work because the size is a numeric -- and is found before the `'x'` *(not sure what will be done with the `'x'` and what follows -- but, anyway, that part of the keys is useless, as it's purely redondant information)*
您需要按键自然排序,可以使用 uksort
you need natural sort by keys, can use uksort
尝试使用 ksort()。
编辑:为了使答案完整,请使用
SORT_NUMERIC
标志。Try with ksort().
Edit: to make the answer complete, please do use the
SORT_NUMERIC
flag.如果你看一下 PHP 的 Sorting Arrays 手册页,你可以看到有几个选项可以根据数组的键对数组进行排序,其中 ksort (使用
SORT_NUMERIC
修饰符)很可能就是您想要的。If you take a look at PHP's Sorting Arrays manual page, you can see there are a few options for sorting the array based on its keys, of which ksort (using the
SORT_NUMERIC
modifier) is most likely the one you're after.