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

发布于 2024-09-30 14:58:39 字数 523 浏览 9 评论 0原文

我试图按数组的数字键对数组进行排序,就好像它们不是数字一样——我不想要自然排序。

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

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

发布评论

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

评论(3

你是我的挚爱i 2024-10-07 14:58:39

因为您的数组键是“big-endian”,所以您可以将键显式排序为字符串(覆盖 `sort() 的默认行为以按数字方式对数值进行排序)。 (演示

ksort($arr, SORT_STRING);

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)

ksort($arr, SORT_STRING);
記柔刀 2024-10-07 14:58:39

我不太确定明白你想要什么。
但我猜是这样的:

对数组进行排序

1st : by the first 4 digits of the key
2nd : by the last 2 digits if they're present


$arr = array(
    '100102'  => 'DUMMY123',
    '100100'  => 'test1',
    '1000'    => 'DUMMY',
    '100004'  => 'DUMMY4',
    '100001'  => 'DUMMY1',
    '100002'  => 'DUMMY3',
    '1001'    => 'TEST',
);

function mysort($a, $b) {
    preg_match('/^(\d{4})(\d\d)?$/', $a, $ma);
    preg_match('/^(\d{4})(\d\d)?$/', $b, $mb);

    if ($ma[1] == $mb[1]) {
        if (!isset($ma[2])) $ma[2] = '';
        if (!isset($mb[2])) $mb[2] = '';
        return strcmp($ma[2], $mb[2]);
    }
    return strcmp($ma[1], $mb[1]);
}
uksort($arr, 'mysort');
print_r($arr);

输出:

Array
(
    [1000] => DUMMY
    [100001] => DUMMY1
    [100002] => DUMMY3
    [100004] => DUMMY4
    [1001] => TEST
    [100100] => test1
    [100102] => DUMMY123
)

I'm not really sure understand what you want.
But i guess it's something like that:

this sort the array

1st : by the first 4 digits of the key
2nd : by the last 2 digits if they're present


$arr = array(
    '100102'  => 'DUMMY123',
    '100100'  => 'test1',
    '1000'    => 'DUMMY',
    '100004'  => 'DUMMY4',
    '100001'  => 'DUMMY1',
    '100002'  => 'DUMMY3',
    '1001'    => 'TEST',
);

function mysort($a, $b) {
    preg_match('/^(\d{4})(\d\d)?$/', $a, $ma);
    preg_match('/^(\d{4})(\d\d)?$/', $b, $mb);

    if ($ma[1] == $mb[1]) {
        if (!isset($ma[2])) $ma[2] = '';
        if (!isset($mb[2])) $mb[2] = '';
        return strcmp($ma[2], $mb[2]);
    }
    return strcmp($ma[1], $mb[1]);
}
uksort($arr, 'mysort');
print_r($arr);

Output:

Array
(
    [1000] => DUMMY
    [100001] => DUMMY1
    [100002] => DUMMY3
    [100004] => DUMMY4
    [1001] => TEST
    [100100] => test1
    [100102] => DUMMY123
)
也只是曾经 2024-10-07 14:58:39

无论您添加多少个额外的 2 字符子类别,asort() 都应该能够解决问题。使用 SORT_STRING 标志,类别甚至不必是字符串。

$arr =('100001'=>'DUMMY1',
       '1000'=>'DUMMY',
       '1001'=>'TEST',
       '100002'=>'DUMMY3',
       '100004'=>'DUMMY4',
       '100102'=>'DUMMY123',
       '100100'=>'test1');

asort($arr, SORT_STRING);

应该导致

$arr =('1000'=>'DUMMY',
       '100001'=>'DUMMY1',
       '100002'=>'DUMMY3',
       '100004'=>'DUMMY4',
       '1001'=>'TEST',
       '100100'=>'test1',
       '100102'=>'DUMMY123');

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.

$arr =('100001'=>'DUMMY1',
       '1000'=>'DUMMY',
       '1001'=>'TEST',
       '100002'=>'DUMMY3',
       '100004'=>'DUMMY4',
       '100102'=>'DUMMY123',
       '100100'=>'test1');

asort($arr, SORT_STRING);

Should result in

$arr =('1000'=>'DUMMY',
       '100001'=>'DUMMY1',
       '100002'=>'DUMMY3',
       '100004'=>'DUMMY4',
       '1001'=>'TEST',
       '100100'=>'test1',
       '100102'=>'DUMMY123');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文