按字母数字键自然对数组进行排序

发布于 2024-10-29 08:36:55 字数 312 浏览 0 评论 0原文

如何按其键对这样的数组进行排序,从较小的分辨率到较大的分辨率:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟花易冷人易散 2024-11-05 08:36:55

ksort()< /a> 在数字模式应该工作得很好:

$sizes = array(
   '120x120' => 'large',
   '60x60' => 'small',
   '200x200' => 'very large',
   '90x90' => 'medium',
);

ksort($sizes, SORT_NUMERIC);
var_dump($sizes);

会让你:

array
  '60x60' => string 'small' (length=5)
  '90x90' => string 'medium' (length=6)
  '120x120' => string 'large' (length=5)
  '200x200' => string 'very large' (length=10)

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 :

$sizes = array(
   '120x120' => 'large',
   '60x60' => 'small',
   '200x200' => 'very large',
   '90x90' => 'medium',
);

ksort($sizes, SORT_NUMERIC);
var_dump($sizes);

will get you :

array
  '60x60' => string 'small' (length=5)
  '90x90' => string 'medium' (length=6)
  '120x120' => string 'large' (length=5)
  '200x200' => string 'very large' (length=10)

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)*

如若梦似彩虹 2024-11-05 08:36:55

您需要按键自然排序,可以使用 uksort

uksort($array, 'strnatcasecmp');

you need natural sort by keys, can use uksort

uksort($array, 'strnatcasecmp');
恬淡成诗 2024-11-05 08:36:55
$sizes = array(
   '120x120' => 'large',
   '60x60' => 'small',
   '200x200' => 'very large',
   '90x90' => 'medium');
uksort($sizes, 'userSorting');
print_r($sizes);
function userSorting($a, $b) {
    $a = explode('x', $a);
    $b = explode('x', $b);
    return $a[0] > $b[0];
}
$sizes = array(
   '120x120' => 'large',
   '60x60' => 'small',
   '200x200' => 'very large',
   '90x90' => 'medium');
uksort($sizes, 'userSorting');
print_r($sizes);
function userSorting($a, $b) {
    $a = explode('x', $a);
    $b = explode('x', $b);
    return $a[0] > $b[0];
}
烟雨扶苏 2024-11-05 08:36:55

尝试使用 ksort()

按键对数组进行排序,并维护键
到数据相关性。这很有用
主要用于关联数组。

编辑:为了使答案完整,请使用 SORT_NUMERIC 标志。

Try with ksort().

Sorts an array by key, maintaining key
to data correlations. This is useful
mainly for associative arrays.

Edit: to make the answer complete, please do use the SORT_NUMERIC flag.

荭秂 2024-11-05 08:36:55

如果你看一下 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文